You are on page 1of 24

LOOPS

in

Python
Flow of Control
Flow of control can be divided into three categories :

1) Sequential
2) Selection
3) Iteration

Iteration means execution of a block of statements , multiple


number of times. Also called as Looping.

Loops statements an be used if we need to run same code


again and again, each time with a different value.
There are two types of iteration:

-Definite iteration, in which the number of repetitions is specified explicitly in


advance

-Indefinite iteration, in which the code block executes until some condition is
meet. In Python, indefinite iteration is performed with a while loop.

The Loops structures in python are:

• for loop

• for – else structure

• 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.

The general syntax is :

for <variable> in range (low , high):

block of statements (with indent)

Ex: for i in range (1,11):

print (i)

Will generate numbers from 1 to 10 (10 values)


For Loop structure :

for <variable> in range (low , high):


block of statements

Ex: for i in range (1,11) :


print (i)
In the loop structure :
i : is the control variable whose value will change. range( ) : is a function
1 : is the initialization value , 11 : is the one increment value of last number till wants the
numbers to be print
Generally for loop follows automatic increment, but step size for increment can be specified.
For Loop structure :

Ex program :
Program to print numbers from 1 to 50.
for i in range (1,51) :
print ( i , end=“ “)

Program to print numbers from 1 to n where n is entered by user.


n = int(input(“Enter the end limit”))
for i in range (1,n+1) :
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)

Program to print sum of numbers from 1 to n where n is entered by user.


n = int(input("Enter the end limit"))
s=0
for i in range (1,n+1) :
s=s+i
print(i,end=" ")
print("sum of series is =",s)
Practice program :

• WAP to enter both the limits and calculate the sum of the values between them.

Ex. : Program to print the multiplication of numbers between 1 to 5.

m=1
for i in range (1,6): //will multiply from 1 to 5
m=m*i
print (m)

This is also called as a FACTORIAL of a number.

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=" ")

Program to print a table of number entered by the user.

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=“ “)

• Program to print the squares of numbers from 1 to n (where n is user entered).


m=1
n = int(input(“Enter upper limit “ ))
for i in range (1,n+1):
m=i*i
print (m , end=“ “)
•Program to print the sum of squares of number from 1 to 5.

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 :

for <variable> in range (low , high, step):


block of statements (with indent)
Step 1 is always by default in for loop.

Ex: for i in range (1,10,2):


print (i)
Will generate numbers as 1,3,5,7,9 (odd numbers)

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 :

for <variable> in range (high , low, -step):


block of statements (with indent)

• Ex: for i in range (10,1,-2):


print (i)
Will generate numbers as 10,8,6,4,2 (even numbers in reverse order.)

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 :

WAP to print the factorial of a user entered number.

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 :

WAP to print the Fibonacci series (0,1,1,2,3,5,8,13…..) for n term (number of


elements to be generated not the end limit).

n=int(input("Enter number of terms to print"))


a=0
b=1
print(a,b,end=" ")
for i in range(1,n+1):
c=a+b
print(c,end=" ")
a=b
b=c
• Nested For Loop :
A nested loop is a loop that occurs within another loop, structurally similar
to nested if statements.
Syntax :
for <variable> in range (low , high): # outer loop
<statement block>
for <variable> in range (low , high) : # inner loop
<statement block>
-The program first encounters the outer loop, executing its first iteration.
-This first iteration triggers the inner loop, which then runs completely.
-Program returns back to the top of the outer loop, for the second iteration
and
-Again triggers the inner loop and so on till outer loop ends.
• Practice programs :

WAP to print the value of power of a number entered by user


(without using pow() or **)

b=int(input("Enter base number")) Output


p=int(input("Enter power")) Enter base number:3
Enter power:5
val=1 3
9
for i in range(1,p+1): 27
81
val=val*b 243
print(val)
• Practice programs for nested for loop: Output:
1
WAP to print the series: 11 , 22 , 33 , 44 , 55 4
27
for i in range(1,6): 256
ans=1 3125

for p in range(1,i+1):
ans=ans*i
print(ans)

• Program to sum them also: 11 + 22 + 33 + 44 + 55 Output:


sum=0 3413
for i in range(1,6):
ans=1
for p in range(1,i+1):
ans=ans*i
sum=sum+ans
print(sum)
• Practice programs :

WAP to print a pattern : 1


12
123
1234
12345

for i in range (1,6):


for j in range(1,i+1):
print(j,end=" ")
print(" ")
Practice programs :
WAP to check number entered is prime or not(prime number gives zero
twice only after division).

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")

You might also like