LESSON 5-
FLOW OF CONTROL
1) Empty statement
• The simplest statement is the empty statement means do nothing.
• Python pass statement is the empty statement.
• Means it is used when a statement is required syntactically but you
do not want any command/code to execute.
• The pass statement is a null operation.
• EG: i =int(input(“enter no “))
if i>10:
pass
else:
print(“i less than 10”)
Types of statements in Python
• Statements are instructions given to the computer to perform some
kind of action.
• Statements form the smallest executable unit within a python
program.
• Following are 3 types of statements:
1) Empty statement
2) Simple statement i.e. single statement
3) Compound statement
2) Simple statement
• Any single executable statement is a simple statement in python.
• EG: nm=input(“Enter ur name ”)
print(“hello ”, nm)
3) Compound statement
• Compound statement is a group of statements executed as a unit. It
is like block of code.
• It has header ending with colon (:) and body containing sequence of
statements.
• Mostly used in if statement, for-loop and while-loop.
• EG: i =int(input(“enter no “))
if i>10: header
print(i) Body block
print(“i greater than 10”)
Statement flow control
Statement flow control
Selection Iteration i.e.Loops
Sequence
(if statement) for-loop, while-loop
1)Sequence
• Statements/code execute one after the other is called sequence.
• EG:
X=int(input(“enter no”))
Y=X*X
print(“square =“ , Y)
2) Selection
• Selection also called decision construct because it helps in making
decision and then execute set of code.
• EG:
• Either of statements will execute; makes decision
Flow chart on if…statement…
Syntax:
if <condition> :
statement1
statement2
Flow chart on if…else statement…
Syntax:
if <condition> :
statement1
statement2
else:
statement1
statement2
Flow chart on if…elif… statement…
Syntax:
if<condition> :
statement(s)
elif <condition>:
statement(s)
else:
statement(s)
Programs:
• WAP to input a age from user and print whether user is eligible voter
or not.
• WAP input a number from user and print whether no. is +ve/-ve or
zero.
• WAP to input a single character from user. If character falls in
between 0 to 9 then display “you entered digit” else display “you
entered char” and if user enters space then display “you entered
space”
Executing multiple statements
This code gives ERROR ; in case if
condition is false
Checking and comparing string literal
S=input("Enter String ")
if S=='Apple’: #single quotes
print("fruit")
OR
if S=="Apple": #double quotes
print("fruit")
Comparing two variables:
A=int(input("enter no 1 "))
B =int(input("enter no 2 "))
if A>B: #values of variables are compared
print(“A is Big”)
Checking/comparing truth value is True/False
x=input("enter data ")
if x:
print("truth value of x is True")
else:
print("truth value of x is False")
NOTE: if variable has some value then truth value of variable is True
else truth value is False. Any value is true i.e. It can be space/ 0 bcoz
space/0 is considered as string.
Truth value of x is False only when x has NO value
Same program using not logical operator
x=input("enter data ")
if not x:
print("there is data so truth value of x is False")
else:
print("No data so truth value of x is True")
WAP to input sales from user. If sales >10000 discount on sales should be
10% else 5%. Display discount, discounted amt and payable amt
i.e. sales-disc.
sales=float(input("enter sales "))
if sales>10000:
disc_amt=sales*0.10
disc="10%"
else:
disc_amt=sales*0.05
disc="5%“
print("your sales ",sales)
print("discount offer :",disc)
print("discount amt :",disc_amt)
print("net payable amt",(sales-disc_amt))
• WAP to input 3 nos from user and print maximum no. Use only if statement
x=float(input("enter no1: "))
y=float(input("enter no1: "))
z=float(input("enter no1: "))
print(max(x,y,z))
Note: max() function takes any no of arguments of type int/float
• WAP to input 3 nos from user and print maximum no.
x=float(input("enter no1: "))
y=float(input("enter no1: "))
z=float(input("enter no1: "))
max=x
if y>max:
max=y
if z>max:
max=z
print("largest no is ",max)
• WAP to input int no. from user and print whether no. is ODD or EVEN
#solution1
no=int(input("enter no1: "))
if no%2==0: #solution3
print(no, "is EVEN number") no=int(input("enter no1: "))
else: if no%2==1:
print(no, "is ODD number")
print(no, "is ODD number")
#solution2
else:
no=int(input("enter no1: "))
if no%2!=0:
print(no, "is EVEN number")
print(no, "is ODD number")
else:
print(no, "is EVEN number")
Program for
• Program 5.3 PG146
practical work
2,3,4 s1(2+3+4) =9 s2(2+3+4)=9
3,2,3 s1(3+2+3) =8 s2(2) =2
sum2+=num2 sum2=sum2+num2 = 0+2 = 2
4,4,4 s1(4+4+4)=12 s2() =0
• Program 5.4 PG148
• Program 5.5 PG148
• Program 5.6 PG149
• Program 5.7 PG 150
WAP to input sales from user and calculate disc as per following:
Sales disc
>=30000 18%
>=20000 15%
>=10000 10%
Less than 10000 5%
Calculate disc and display discounted amt and discount. Also display net
amount ie sales-discounted amt.
sales=int(input("enter sales "))
sales=int(input("enter sales "))
if sales>=30000:
if sales>=30000:
disc="18%"
disc="18%"
disc_amt=sales*0.18
disc_amt=sales*0.18
elif sales>=20000: elif sales<30000 and sales>=20000:
disc="15%" disc="15%"
disc_amt=sales*0.15 disc_amt=sales*0.15
elif sales>=10000: elif sales<20000 and sales>=10000:
disc="10%" disc="10%"
disc_amt=sales*0.10 disc_amt=sales*0.10
else: elif sales<10000:
disc="5%" disc="5%"
disc_amt=sales*0.05 disc_amt=sales*0.05
print("Total sales", sales) print("Total sales", sales)
print("Discount ",disc) print("Discount ",disc)
print("Discounted amount ", disc_amt) print("Discounted amount ", disc_amt)
print("Net Payable amt ", (sales-disc_amt)) print("Net Payable amt ", (sales-disc_amt))
Input a character from user and print whether the character is a vowel or
consonant . Using or logical operator in if..condn
s=input("Enter single char ")
s=s.lower() #converting all chars to lower case
s=s[0] #storing only single char in object s
if(s=='a' or s=='e' or s=='i' or s==‘o' or s=='u'):
print(s, "is a vowel")
elif (s>='0' and s<='9'):
print(s, "is a number ")
elif s==' ':
print(s,"is a space")
else:
print(s, "is a consonant")
Input a character from user and print whether the character is a vowel or
consonant . Using or logical operator in if..condn
s=input("Enter single char ")
s=s[0] #storing only single char in object s
if(s=='a’ or s=='e' or s=='i' or s==‘o' or s=='u’):
print(s, "is a vowel")
elif (s>='0' and s<='9'):
print(s, "is a number ")
elif s==' ':
print(s,"is a space")
else:
print(s, "is a consonant")
Input a no. from user and print day of week. EG: 1=Sunday, 2=Monday.… if no>7 or < 1 print invalid day.
no=int(input("Enter no "))
if no==1:
s="Sunday"
elif no==2:
s="Monday"
elif no==3:
s="Tueday"
elif no==4:
s="Wednesday"
elif no==5:
s="Thursday"
elif no==6:
s="Friday"
elif no==7:
s="Saturday"
else:
s="Invalid day"
print(s)
WAP to input year from user and print whether it is a leap yr or leap yr
and century yr or non-leap yr.
WAP to input three nos from user and print them in ascending order
Iteration: means repetition of tasks. Each time, when
the loop-body is executes, is called and iteration
Python offers two iterations i.e. for-loop and while loop.
I) for-loop: programmer executes this loop when he wants to execute
a loop-body for fixed no. of times. Also called as counting loop.
range( ) function in for –loop
Syntax:
range(lower_limit, upper_limit-1, step_val)
OR
range(upper_limit-1) #default starts from 0, so lower_limit will be 0
#Program1 WAP to print nos 0-9
for a in range(10):
print(a)
[Note here variable a is declared to zero(0) and is called iteration
variable and goes upto upper_limit-1. so here it goes till 9
WA for-loop to print odd nos between 1-10
for a in range(1,11,2): Step val
print(a)
WORKING o/p
a=1 range =11 check point 1
a=1+2=3 3
a=3+2=5 5
a=5+2=7 7
a=7+2=9 9
a=9+2=11 X (loop terminates)
WA for-loop to print odd nos between 1-10
for a in range(1,11):
if a%2==1:
print(a)
#NOTE: when no step val given then it takes a default
Working (Dry-run)
a=a+1 range=check-point if condn o/p
1 y T 1
2 Y F
3 Y T 3
4 Y F
5 Y T 5
6 Y f
7 Y t 7
8
WA for-loop to print EVEN nos between 1-10
a o/p
for a in range(2,11,2):
print(a) 2+2=4 4
4+2=6 6
6+2=8 8
8+2=10 10
OR
for a in range(1,11): 2
if a%2==0: 4
print(a) 6
8
#NOTE: default step value is 1 10
WA for-loop to print odd nos between 1-10 and their sum. Print sum of all odd nos at the end.
s=0
for a in range(1,11,2):
print(a)
s=s+a
print("sum",s)
WORKING
s=0 a=1 range =11 check point o/p
s=s+a a=a+2(step val) 1
3
0+1=1 a=1+2=3 5
1+3=4 a=3+2=5 7
4+5=9 a=5+2=7 9
9+7=16 a=7+2=9 sum 25
16+9=25 a=9+2=11 X (loop terminates)
WA for-loop to print EVEN nos between 1-10 and their sum. Print sum of all
EVEN nos at the end.
s=0
for a in range(2,11,2):
print(a)
s=s+a #can be replace with s+=a
print("sum",s)
OR
s=0
for a in range(1,11):
if a%2==0:
print(a)
s+=a #means s=s+a
print("sum=",s)
WA for-loop to print nos from 5-10
for a in range(5,11):
print(a)
WA for-loop to print nos from 25-50
for a in range(25,51):
print(a)
Using ‘in’ / ‘not in’ operator in loop:
The in operator tests if a given values is contained in a sequence or not.
Returns True if, value exits else returns False. Operators in and not in are
membership operators.
Lets understand this:
3 in [1,2,3,4] o/p is True #b’coz 3 is an element of list
5 in [1,2,3,4] o/p is False #b’coz 5 is not an element of list
‘ ‘ ’ in ‘h’ello’ o/p is True #b’coz h is part of hello string (case
sensitive)
5 not in [1,2,3,4] o/p is True WHY?? As 5 is not in list
Syntax:
for <variable_name> in <sequence>:
print()
PROGRAM on list sequence
for a in [1,4,7]: #[1,4,7] is list sequence
print(a) NOTE:
1.Here, ‘a’ is loop variable.
OR 2.Variable ‘a’ will be assigned
each value of list one by one.
3.First value of a is 1, the 4 and
L1=[1,4,7]
then 7.
for a in L1: # L1 is list sequence
4.The loop executes 3 time.
print(a)
#PROGRAM on string sequence.
s='apple'
for ind in s:
print(ind) # will print single character on each line
s='apple'
s1='red'
for ind in s+s1: #can join the sequence
print(ind) #will print chars of s and then s1 string on different line
NOTE: for-loop ends when the loop is repeated for the last values of the
sequence.
L=['ram','ravan','sita']
for ind in L: #L is a list sequence of strings. Will print each
print(ind) element on different line
T=('ram','ravan','sita’) #T is tuple
for ind in T:
print(ind)
T=(1,2,3)
for ind in T:
print(ind*ind)
Find the output
for i in [1,2,3]:
print(i*i*i)
WORKING
i loopbody o/p
1 1*1*1 1
2 2*2*2 8
3 3*3*3 27
WA for-loop to print the following series using sequence
1 4 9 16 25 36 …..100
L=[1,2,3,4,5,6,7,8,9,10]
for val in L:
print(val*val, end=‘ ’)
Note study the series is one line.
How will u run same program without sequence ??
for val in range(1,11):
print(val*val,end=' ')
WA for-loop to input a number for user and print its table till 10.
no=int(input("enter no "))
for a in range(1,11):
print(no,"X",a,"=",(no*a))
Parameters of range() can be only integers
for a in range(1.0,11,0.5):
print(a)
#ERROR 'float' object cannot be interpreted as an integer
range() takes only integers as parameters.
WA for-loop to print the series 1.0 1.5 2.0 2.5..... till ten terms
ser=1.0
for a in range(10):
print(ser,end=' ')
ser+=0.5
# WAP to print Fibonacci series upto 10
terms
n1=0
n2=1
n3=0
print(n1, n2,end=' ')
for a in range(8):
n3=n1+n2
print(n3, end=' ')
n1=n2
n2=n3
WA for-loop to input no from user and print the factorial of a number
no=int(input("Enter no "))
fact=1
for a in range(1,no+1):
fact=fact*a
print("factorial of ",no,"is",fact)
Reverse for-loop
syntax:
range(start_index, end_limit+1, step_val)
NOTE: when you want to print reverse nos then step value should be -1.
if you don’t give step val will not give an error but will not execute the
loop.
WA for-loop to print nos from 5 to 1
for a in range(5,0,-1):
print(a)
WORKING. Endlimit+1 i.e. 0+1 =1
a loop-body (checkpoint a=1 OR a>0) o/p
5 yes/True 5
a=a-1(5-1)=4 y 4
a=a-1(4-1)=3 y 3
a=a-1(3-1)=2 y 2
a=a-1(2-1)=1 y 1
WA for-loop to print odd nos WA for-loop to print Even nos
from 10-1 and their sum from 10-1 and their sum
s=0 s=0
for a in range(9,0,-2): for a in range(10,0,-2):
print(a) print(a)
s=s+a s=s+a
print("sum",s) print("sum",s)
WA for-loop to print odd nos WA for-loop to print Even nos
from 10-1 and their sum from 10-1 and their sum
s=0 s=0
for a in range(9,0,-1): for a in range(10,0,-1):
if a%2==1: if a%2==0:
print(a) print(a)
s=s+a s=s+a
print("sum",s) print("sum",s)
WA for-loop to input no from user and print the factorial of a number
#calculating from no to 1(reverse)
#reverse fact
no=int(input("Enter no "))
fact=1
for a in range(no, 1, -1):
fact=fact*a
print("factorial of ",no,"is",fact)
WAP to print the sum of even and odd nos of first n natural nos.
no=int(input("Enter no "))+1
print(no)
ev=odd=0
for a in range(1,no):
if a%2==0:
ev+=a
else:
odd+=a
print("sum of even nos",ev)
print("sum of odd nos",odd)
Find output:
n=1
n=1 x=2
for a in range(5): for a in range(5):
print(n) print(n)
n=n*10+1 n=n*10+x
x=x+1
WAP to print the following pattern:
s='*'
for a in range(1,5):
print(s*a)
s=''
for x in '****':
s=s+x
print(s)
#Q9 Pg178
n=int(input("how many nos u want to enter "))
s=0
z=1
for a in range(n):
print("enter no ",z,end='')
z=z+1
x=int(input(""))
s=s+x
avg=s/n
print("avg = ",avg)
• WAP to input the no. from the user and print the sum of its digits.
EG if no is 123 the it should print 6 i.e.1+2+3
Solution1:
no= int(input("enter num "))
s=0
for a in str(no):
s=s+int(a)
print(s)
• WAP to input the no. from the user and print the sum of its digits.
EG if no is 123 the it should print 6 i.e.1+2+3
Solution2:
no= int(input("enter num "))
le=len(str(no))
s=0
for a in range(1,le+1):
r=no%10
s=s+r
no=int(no/10)
print(s)
WAP to input a no from a user and print the reverse of
entered no.
num= int(input("enter num "))
rev = 0
print("Given Number ", num)
le=len(str(num))
for x in range(1,le+1):
reminder = num % 10
rev= (rev * 10) + reminder
num = num // 10
print("Revered Number ", rev)
Iteration: means repetition of tasks. Each time, when
the loop-body is executes, is called and iteration
Python offers two iterations i.e. for-loop and while loop
II) while-loop: programmer executes this loop when he wants to
execute a loop-body till the condition is true. While loop is condition
based loop. Also called as conditional loop. The loop that repeats as
long as condition is true is a while loop
Syntax: