GEC PYTHON ASSIGNMENT
( B.A. English honours)
( Roll number: )
Q1. Complete/write the following code:
a) Print "1" if a is equal to b, print "2" if a is greater than b, otherwise print "3".
Ans : if a == b :
print(“1”)
elif a > b:
print(“2”)
else:
print(“3”)
b) Print "Hello" if a is equal to b, and c is equal to d.
Ans : if (a == b) and (c == d):
print(“Hello”)
c) Stop the loop if i is 3.
i=1
while i < 6:
if i == 3:
..break...........
i += 1
d) Evaluate: 1 + 2 + 3 + 4.0 + 5 = 15.0
e) Output of the following code:
def my_function(fname):
print(fname + " Rose")
my_function("Emily")
my_function("Tobias")
Output: EmilyRose
TobiasRose
f) Use the correct logical operator to check if at least one of two statements is True.
if 5 == 10 or 4 == 4:
print("At least one of the statements is true")
g) Evaluate: 7%(5 // 2) = 1
h) What is dot pitch?
Ans : The dot pitch or pixel pitch is a measurement that defines the sharpness of a display. The
dot pitch is measured in millimeters (mm) and a smaller the dot pitch sharper is the display.
Q2. a) Write a python function to find sum = 1+2+3+...+n.
Ans : def sum(num):
sum = 0
for i in range(1, num+1):
sum = sum + i
print("Sum of numbers is :", sum)
def main():
n = int(input("Enter the number :"))
sum(n)
main()
b) Which function is used for the following:
i) absolute value of a number say x
Ans : abs() function
ii) to round off the number
Ans : round() function
iii) to evaluate the value of a string
Ans : eval() function
Q3. Write the python program that inputs a number from the user, calculates its reverse
and sum of its digits and displays the output to the user? ( using functions)
Ans: def reverse_digit(n):
rev=0
sum=0
while n>0:
r=n%10
n=n//10
rev= rev*10+r
sum+=r
print("The reversed number is: " , rev)
print("The sum of digits is: " , sum)
def main():
num = int(input("Enter a number"))
reverse_digit(num)
if __name__=='__main__':
main()
Q4. Write the python code that inputs two numbers from the user and returns their least
common multiple (LCM), use functions?
Ans: def lcm(a,b):
if a > b:
greater = a
else:
greater = b
while (True):
if (greater % a == 0)and( greater % b == 0):
lcm=greater
break
greater+=1
print("The LCM of the numbers is:", lcm)
def main():
x = int(input("Enter 1st number"))
y = int(input("Enter 2nd number"))
lcm(x,y)
if __name__=='__main__':
main()
Q5. a) Differentiate between the following operators with the help of an example:
1. = and ==
Ans: = is simply an assignment operator. It assign value to a variable.
Ex: a = 10 (here value 10 is assigned to variable a)
== is a relational operator. It compares the value of two operands to be
equal or not and return true or false accordingly.
Ex: a == 10 (here it check is the value of a is 10 or not)
2. / and //
Ans: / is a arithmetic operator that return the quotient of the operands in decimal form.
Ex: 10/3 = 3.333333333333….
// is a arithmetic operator, that return the quotient of the operands in integral form.
Ex: 10//3 = 3 (no decimal part is displayed)
b) Write a function to print the following pattern: (input no. of lines from the user)
1
22
333
4444
Program:
def pattern(n):
for i in range (1, n+1):
for j in range(1,i+1):
print(i, end='')
print()
def main():
num = int(input("Enter a number"))
pattern(num)
if __name__=='__main__':
main()