Professional Documents
Culture Documents
in
Python
Flow of Control
Flow of control can be divided into three categories :
1) Sequential
2) Selection
3) Iteration
-Indefinite iteration, in which the code block executes until some condition is
meet. In Python, indefinite iteration is performed with a while loop.
• for loop
• while loop
For Loop structure :
range ( ) function : If you need to iterate over a sequence of numbers, the built-in function
range () comes handy . It generates numbers in a sequence. A for loop can iterate over a
sequence of list , tuples, string and so on …which we will learn later on.
print (i)
Ex program :
Program to print numbers from 1 to 50.
for i in range (1,51) :
print ( i , end=“ “)
Practice prog. : WAP to enter both the limits and print the values
between them.
Program to print the sum of numbers from 1 to 50.
s=0
for i in range (1,51) :
s=s + i
print(i,end=" ")
print()
print("sum of numbers from 1 to 50:",s)
• WAP to enter both the limits and calculate the sum of the values between them.
m=1
for i in range (1,6): //will multiply from 1 to 5
m=m*i
print (m)
1 x 2 x 3 x 4 x 5 = 120
Program to print the table of number 3.
m=1
for i in range (1 , 11) :
m=3*i
print (m, end=" ")
m=1
n = int(input("Enter number for table generation"))
for i in range (1,11):
m = n*i
print (m , end=" ")
• Program to print the squares of number from 1 to 5.
for i in range (1 , 6) :
m=i * i
print (m, end=“ “)
s=0
for i in range (1,6):
s=s+(i * i)
print("Sum of squares is =",s)
Practice programs:
• Program to print the sum of squares of numbers from 1 to n (where n is
user entered).
• Program to print the sum of cubes of numbers from 1 to n (where n is user
entered).
For Loop with step :
The general syntax is :
Practice programs :
• WAP to print even numbers from 1 to 50.
• WAP to print even numbers from 1 to n (where n is user entered)
• WAP to print odd numbers from 1 to n.
• Reverse For Loop with (–)ve step :
• The general syntax is :
Practice programs :
• WAP to print numbers in reverse order from 20 to 1.
• WAP to print numbers in reverse order from (n) to 1.
• Practice programs :
WAP to print sum of even number and odd numbers individually
between 1 to 50.
se=0
so=0
for i in range(1,51):
if i%2==0:
se=se+i
else:
so=so+i
print("Sum of odd numbers is :",so)
print("Sum of even numbers is:",se)
Practice programs :
ft=1
n = int (input("Enter number for factorial"))
for i in range (1,n+1):
ft=ft*i
print ("Factorial of the number is :",ft)
• Practice programs :
for p in range(1,i+1):
ans=ans*i
print(ans)
count=0
n=int(input("enter number to check"))
for i in range(1,n+1):
if(n%i==0):
count=count+1
if count<=2:
print("Prime number")
else:
print("Not a Prime Number")
For Loop with else clause :
-A for loop can have an optional else block as well.
-The else part is executed if the items in the sequence used in for loop
exhausts.
-The break keyword can be used to stop a for loop. In such cases, the else part
is ignored.
-Hence, a for loop's else part runs if no break occurs.
Ex 1:
for x in range(5):
print ("iteration no {} in for loop".format(x+1)) else:
print ("else block in loop")
print ("Out of loop")
for i in range (0,10):
print (i)
else:
print("all numbers are printed")
Ex 2: (just for Else clause )
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(n)
break
else:
print(n,"is a prime number")