2. âą To gain knowledge on the various
flow of control in Python language.
âą To learn through the syntax how
to use conditional construct to
improve the efficiency of the
program flow.
âą To apply iteration structures to
develop code to repeat the program
segment for specific number of
times or till the condition is
satisfied.
4. âą Programs may contain set
of statements.
âą These statements are the
executable segments that
yield the result.
âą In general, statements are
executed sequentially, that
5. âą There may be situations
in our real life
programming where we
need to skip a segment
or set of statements
and execute another
segment based on the
test of a condition.
6. âą Also, we may need to
execute a set of
statements multiple
times, called iteration
or looping.
âą In this chapter we are
to focus on the various
24. a = int (input("Enter
any number :"))
x="even" if a%2==0
else "odd"
print (a, " is ",x)
xample
Output 1:
Enter any number :3
3 is odd
Output 2:
Enter any number :22
22 is even
28. m1=int(input("Enter mark in first subject:
m2=int(input("Enter mark in second subje
avg=(m1+m2)/2
if avg>=80:
print ("Grade : A")
elif avg>=70 and avg<80:
print ("Grade : B")
elif avg>=60 and avg<70:
print ("Grade : C")
elif avg>=50 and avg<60:
print ("Grade : D")
else:
print ("Grade : E")
Example
Output 1:
Enter mark in first subject : 34
Enter mark in second subject : 78
Grade : D
Output 2 :
Enter mark in first subject : 67
Enter mark in second subject : 73
Grade : B
36. i = 1
while i < 15:
print(i)
i += 1
Example
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
37. x = 0
while (x < 16):
print(x)
x += 1
else:
print("nValue of i when the loop exit ",x)
Example
Output: 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Value of i when the loop exit 16
39. for loop is the most
comfortable loop.
It is also an entry check
loop.
The condition is checked
in the beginning and the
body of the
loop(statements-block 1)
40. for counter_variable in sequence:
statements-block 1
[else: #
optional block
statements-block 2]
yntax
41. The counter_variable mentioned in the
syntax is similar to the control variable
that we used in the for loop of C++
sequence refers to the initial, final and
increment value.
for loop uses the range() function in the
sequence to specify the initial, final and
increment values. range() generates a
list of values starting from start till
stop-1.
42. The syntax of range() is as follows:
range (start,stop,[step])
Where,
start â refers to the initial value
stop â refers to the final value
step â refers to increment value,
this is optional part.
44. for i in range (2,10,2):
print (i, end=' ')
Output:
2 4 6 8
Example
45. fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Example
output: apple, banana
46. fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
Exampl
e
output: apple
47. Therange()function returns a sequence of
numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a
specified number.
48. for x in range(6):
print(x)
Example
OUTPUT
0
1
2
3
4
5
49. Therange()function defaults to 0 as a
starting value, however it is possible to
specify the starting value by adding a
parameter:range(2, 6), which means
values from 2 to 6 (but not including 6):
50. for x in range(2, 6):
print(x)
Example
OUTPUT
2
3
4
5
51. Therange()function defaults to increment
the sequence by 1, however it is possible to
specify the increment value by adding a third
parameter:range(2, 30,3):
52. for x in range(2, 30, 3):
print(x)
Example
OUTPUT
2
5
8
11
14
17
20
23
26
29
53. for x in range(6):
print(x)
else:
print("Finally finished!")
OUTPUT
0
1
2
3
4
5
Finally finished!
Example
54. for i in range (2,10,2):
print (i, end=' ')
for i in range(2,10,2):
print (i,end=' ')
else:
print ("nEnd of the loop")
n = 10
sum = 0
for counter in range(1,n+1):
sum = sum + counter
print("Sum of 1 until %d: %d" % (n,sum))
Example
55. for word in 'Computer':
print (word,end=' ')
else:
print ("nEnd of the loop")
Example
57. A loop placed within another
loop is called as nested loop
structure.
One can place
âą a while within another while;
âą for within another for;
âą for within while and
âą while within for to construct
such nested loops.
61. The jump statement in Python, is
used to unconditionally transfer the
control from one part of the
program to another.
There are three keywords to
achieve jump statements in Python:
break, continue, pass.
67. Example
for letter in 'Pythonâ:
if letter == 'h':
break
print ('Current Letter :',letter)
OUTPUT:
Current Letter : P
Current Letter : y
Current Letter : t
68. var = 10
while var > 0:
print ('Current variable value :', var)
var = var -1
if var == 5:
break
print ("Good bye!")
Examp
OUTPUT
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!
69. for word in "Jump
Statement":
if word == "e":
break
print (word, end= ' ')
Examp
70. for word in "Jump
Statement":
if word == "e":
break
print (word, end='')
else:
print("End of the loop")
Example
75. Examp
for letter in 'Pythonâ:
if letter == 'h':
continue
print ('Current Letter :',letter)
OUTPUT:
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
76. var = 10
while var > 0:
var = var -1
if var == 5:
continue
print ('Current variable value :',
var)
print ("Good bye!")
Exampl
77. for word in "Jump
Statement":
if word == "e":
continue
print (word, end='')
else:
print("End of the loop")
print("n End of the
80. pass statement in Python programming
is a null statement.
pass statement when executed by the
interpreter it is completely ignored.
Nothing happens when pass is executed,
it results in no operation.
81. for letter in 'Python':
if letter == 'h':
pass
print ('This is pass block')
print ('Current Letter :', letter)
print ("Good bye!")
Exampl
87. a = int (input("Enter number 1"))
b = int (input("Enter number 2"))
c = int (input(" Enter number 3"))
if a > b and a > c:
print ("A is greatest")
elif b > a and b > c:
print ("B is greatest")
else:
print ("C is greatest")
88. for i in range (1, 100, 2):
print (i, end = " ")
Write a program to
display all 3 digit odd
numbers.
89. n = int (input("Enter the
number"))
for i in range (1,13):
print (n, "x", i, "= ", n * i)
Write a program to display
multiplication table for a given
number.
90. x = int (input("Enter a number"))
if x>0:
print (x, "is a positive number")
elif x < 0:
print (x, "is a negative
number")
else:
print (x, "is zero")