ch-9, Flow of Control C.W
ch-9, Flow of Control C.W
TYPE-A
In Python, an empty statement is pass statement. Its syntax is: pass. When pass statement is
encountered, Python does nothing and moves to next statement in the flow of control.
5. What is the difference between else clause of if-else and else clause of Python loops?
The else clause of an if-else statement is executed when the condition of the if statement
results into false. The else clause of a loop is executed when the loop is terminating normally
i.e., when its test condition has become false for a while loop or when the for loop has
executed for the last value in sequence.
6. In which cases, the else clause of a loop does not get executed?
The else clause of a loop does not get executed if the loop is terminated due to the
execution of a break statement inside the loop.
7. What are jump statements? Name them.
Jump statements are used to unconditionally transfer program control to other parts
within a program. Python provides the below jump statements:
1. break
2. continue
1.Write a python script that asks the user to enter a length in centimeters. If the user enters
a negative length, the program should tell the user that the entry is invalid. Otherwise, the
program should convert length to inches and print out the result. There are 2.54 cms in an
inch.
Ans: len=int(input(“Enter length in cm:”))
if len<0:
print(“Invalid Input”)
else:
print(len, ”cm=”, len/2.54, “inches”)
2.A store charges Rs. 120 per item if you buy less than 10 items. If you buy between 10 and
99 items, the cost is Rs. 100 per item. If you buy 100 more items the cost is Rs. 70 per item.
Write a program that asks the user how many items they are buying and prints the total.
Ans:
n = int(input("Enter number of items: "))
cost = 0
if n >= 100 :
cost = n * 70
elif n >= 10 :
cost = n * 100
else :
cost = n * 120
print("Total Cost =", cost)
3. Write a program that reads from user — (i) an hour between 1 to 12 and (ii) number of hours
ahead. The program should then print the time after those many hours, e.g.,
Enter hour between 1-12 : 9
How many hours ahead : 4
Time at that time would be : 1 O'clock
Ans:
4. Write a program to input length of three sides of a triangle. Then check if these sides will
form a triangle or not.
(Rule is: a+b>c;b+c>a;c+a>b)
Ans:
a = int(input("Enter first side : "))
b = int(input("Enter second side : "))
c = int(input("Enter third side : "))
if a + b > c and b + c > a and a + c > b :
print("Triangle Possible")
else :
print("Triangle Not Possible")
7. Write a program to input 3 sides of a triangle and print whether it is an equilateral, scalene
or isosceles triangle.
Ans:
a = int(input("Enter first side : "))
b = int(input("Enter second side : "))
c = int(input("Enter third side : "))
if a == b and b == c :
print("Equilateral Triangle")
elif a == b or b == c or c == a:
print("Isosceles Triangle")
else :
print("Scalene Triangle")
8. Write a program to take N (N > 20) as an input from the user. Print numbers from 11 to N.
When the number is a multiple of 3, print "Tipsy", when it is a multiple of 7, print "Topsy".
When it is a multiple of both, print "TipsyTopsy".
Ans:
n = int(input("Enter a number greater than 20: "))
if n <= 20 :
print("Invalid Input")
else :
for i in range(11, n + 1) :
print(i)
if i % 3 == 0 and i % 7 == 0 :
print("TipsyTopsy")
elif i % 3 == 0 :
print("Tipsy")
elif i % 7 == 0 :
print("Topsy")
9. Write Python programs to sum the given sequences:
12 + 32 + 52 + ..... + n2 (Input n)
Ans:
Ans:
n = int(input("Enter the value of n: "))
sum = 0
for i in range(n + 1) :
fact = 1
for j in range(1, i) :
fact *= j
term = 1 / fact
sum += term
print(“Sum=”, sum)