L5_FLOW OF CONTROL
L5_FLOW OF CONTROL
FLOW OF CONTROL
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
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”)
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
OR
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
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)
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
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
WA for-loop to print nos from 5-10
for a in range(5,11):
print(a)
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=‘ ’)
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)
ser=1.0
for a in range(10):
print(ser,end=' ')
ser+=0.5
# WAP to print Fibonacci series upto 10
terms
x=0
y=1
s=0
print(x, y,end=' ')
for a in range(8):
s=x+y
print(s, end=' ')
x=y
y=s
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)
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:
Syntax: