for more updates visit: www.python4csip.
com
Loop – repetition of task
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
What is Loop?
As we have already studied there are many
situation where we need to repeat same set
of tasks again and again. Like while writing
table of any number we multiply same
number from 1 to 10, multiplying number
from n to 1 to find the factorial, In real life,
to prepare parathas, suppose to prepare
your bag with all items, daily coming to
school again and again, etc. Please explore
more real life conditions where repetition of
task was done by you.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Pseudocode Reference
A. start
B. Input number(n) to print table
C. Let count=1
D. Display n * I
E. Add 1 to count
F. if count==10:
A. Stop
G. else:
A. Repeat from Step C
H. Stop
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Python Loop Statements
To carry out repetition of statements
Python provide 2 loop statements
◦ Conditional loop (while)
◦ Counting loop (for)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
range() function
Before we proceed to for loop let us
understand range() function which we
will use in for loop to repeat the
statement to n number of times.
Syntax:
◦ range(lower_limit, upper_limit)
The range function generate set of values
from lower_limit to upper_limit-1
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
range() function
For e.g.
range(1,10) will generate set of values
from 1-9
range(0,7) will generate [0-6]
Default step value will be +1 i.e.
range(1,10) means (1,2,3,4,5,6,7,8,9)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
range() function
To change the step value we can use third
parameter in range() which is step value
For e.g.
range(1,10,2) now this will generate
value [1,3,5,7,9]
Step value can be in –ve also to generate
set of numbers in reverse order.
range(10,0) will generate number as
[10,9,8,7,6,5,4,3,2,1]
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
range() function
To create list from 0 we can use
range(10) it will generate
[0,1,2,3,4,5,6,7,8,9]
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Operators in and not in
The operator in and not in is used in for
loop to check whether the value is in the
range / list or not
For e.g.
>>> 5 in [1,2,3,4,5]
True
>>> 5 in [1,2,3,4]
False
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
>>>‟a‟ in „apple‟
True
>>>‟national‟ in „international‟
True
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Program to check whether any
word is a part of sentence or not
line = input("Enter any statement")
word = input("Enter any word")
if word in line:
print("Yes ", word, "is a part of ",line)
else:
print("No", word ," is not part of ",line)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
for loop
for loop in python is used to create a
loop to process items of any sequence
like List, Tuple, Dictionary, String
It can also be used to create loop of fixed
number of steps like 5 times, 10 times, n
times etc using range() function.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example – for loop with List
School=["Principal","PGT","TGT","PRT"]
for sm in School:
print(sm)
Example – for loop with Tuple
Code=(10,20,30,40,50,60)
for cd in Code:
print(cd)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us understand how for loop works
Code=(10,20,30,40,50,60)
for cd in Code:
print(cd)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
First it will create Tuple „Code‟
Code=(10,20,30,40,50,60)
for cd in Code:
print(cd)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Then for loop starts, it will pick values one
by one from Code and assign it to cd
Code=(10,20,30,40,50,60)
for cd in Code:
print(cd)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Then for loop starts, it will pick values one
by one from Code and assign it to cd
Code=(10,20,30,40,50,60)
for cd in Code: cd = 10
print(cd)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Then for loop starts, it will pick values one
by one from Code and assign it to cd
Code=(10,20,30,40,50,60)
for cd in Code: cd = 10
print(cd)
OUTPUT
10
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Then for loop starts, it will pick values one
by one from Code and assign it to cd
Code=(10,20,30,40,50,60)
for cd in Code: cd = 20
print(cd)
OUTPUT
10
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Then for loop starts, it will pick values one
by one from Code and assign it to cd
Code=(10,20,30,40,50,60)
for cd in Code: cd = 20
print(cd)
OUTPUT
10 FOR LOOP WILL
20 AUTOMATICALLY ENDS
WHEN LAST ITEM OF
AND SO ON… LIST IS PROCESSED
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
for loop with string
for ch in „Plan‟:
print(ch)
The above loop product output
P
l
a
n
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
for with range()
Let us create a loop to print all the natural number from 1
to 100
for i in range(1,101):
print(i,end='\t')
** here end=„\t‟ will cause output to appear without
changing line and give one tab space between next output.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Program to print table of any number
num = int(input("Enter any number "))
for i in range(1,11):
print(num,'x',i,'=',num*i)
Program to find the sum of all number divisible by 7 between 1 to 100
sum=0
for i in range(1,101):
if i % 7 == 0:
sum+=i
print("total of number divisible by 7 between 1 to 100 is ",sum)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Lab work
1. WAP to enter any number and find its
factorial
2. WAP to print the following fibonacci series
0, 1, 1, 2, 3, 5, 8,……..n terms
3. WAP to enter 10 number and find the sum
and average.
4. WAP to enter Lower_Limit, Upper_Limit
and find the sum of all odds and evens
number between the range separately
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
while loop
While loop in python is conditional loop
which repeat the instruction as long as
condition remains true.
It is entry-controlled loop i.e. it first
check the condition and if it is true then
allows to enter in loop.
while loop contains various loop
elements: initialization, test condition,
body of loop and update statement
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
while loop elements
1. Initialization : it is used to give starting
value in a loop variable from where to
start the loop
2. Test condition : it is the condition or last
value up to which loop will be executed.
3. Body of loop : it specifies the
action/statement to repeat in the loop
4. Update statement : it is the increase or
decrease in loop variable to reach the test
condition.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example of simple while loop
i=1
while i<=10:
print(i)
i+=1
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example of simple while loop
i=1 initialization
while i<=10: Test condition
print(i) Body of loop
i+=1
U
p
d
a
t
e
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10:
print(i)
i+=1
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10: i=1
1<=10?
print(i) True
i+=1
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10: i=1
1<=10?
print(i) True
OUTPUT
---------- i+=1
1
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10:
print(i)
i=2
OUTPUT
---------- i+=1
1
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10: i=2
2<=10?
print(i) True
OUTPUT
---------- i+=1
1
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10: i=2
2<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10:
print(i)
i=3
OUTPUT
---------- i+=1
1
2
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10: i=3
3<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10: i=3
3<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
3
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10:
print(i)
i=4
OUTPUT
---------- i+=1
1
2
3
In this way loop will execute for
the values 4 to 10, let us see from
the value 9
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10:
print(i)
i=9
OUTPUT
---------- i+=1
1
2
3
4
5
6 In this way loop will execute for
7
8
the values 4 to 10, let us see from
the value 9
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10: i=9
9<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
3
4
5
6
7
8
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10: i=9
9<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
3
4
5
6
7
8
9
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10:
print(i)
i=10
OUTPUT
---------- i+=1
1
2
3
4
5
6
7
8
9
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10: i= 10
10<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
3
4
5
6
7
8
9
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10: i = 10
10<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
3
4
5
6
7
8
9
10
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10:
print(i)
i=11
OUTPUT
---------- i+=1
1
2
3
4
5
6
7
8
9
10
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Let us see the flow of loop
i=1
while i<=10: i= 11
11<=10?
print(i) False
OUTPUT
---------- i+=1
1
2
3
4 Now the value of i
5 is 11 and condition
6
7 will return False so
8 loop will terminate
9
10
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Programs of while loop
Convert all „for‟ loop program with while loop
WAP to enter any number and find its reverse
WAP to enter any number and a digit and count
how many times digit is in the number
WAP to enter any number and check it is
armstrong or not
WAP to enter any number and check it is
palindrome or not
WAP to enter numbers as long as user wishes to
enter and find the sum highest and lowest
number entered by user
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
WAP to enter any number and find its reverse
num = int(input("Enter any number "))
rev = 0
num2=num
while num>0:
rem = num % 10
rev = rev * 10 + rem
num //=10
print("Reverse of ",num2," is ",rev)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
WAP to enter numbers as long as user wishes to enter and
find the sum highest and lowest number entered by user
choice="y"
i=1
while choice=="y":
num = int(input("Enter any number (>0)"))
if i==1:
low = num
high = num
i+=1
else:
if (high<num):
high=num
if (num<low):
low = num
choice=input("Enter continue or not (y/n)")
print("Highest value was ",high)
print("Lowest value was ",low)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Jump Statements – break & continue
break statement in python is used to
terminate the containing loop for any
given condition. Program resumes from the
statement immediately after the loop
Continue statement in python is used to
skip the statements below continue
statement inside loop and forces the
loop to continue with next value.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example – break
for i in range(1,20):
if i % 6 == 0:
break
print(i)
print(“Loop Over”)
The above code produces output
1
2
3
4
5
Loop Over
when the value of i reaches to 6 condition will becomes True
and loop will end and messageVINOD
“Loop Over”
KUMAR will be
VERMA, PGT(CS), printed
KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example – continue
for i in range(1,20):
if i % 6 == 0:
continue
print(i, end = „ „)
print(“Loop Over”)
The above code produces output
1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 19
Loop Over
when the value of i becomes divisible from 6,
condition will becomes True and loop will skip all the
statement below continue and continues in loop with
next value of iteration.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Loop .. else .. Statement
Loop in python provides else clause with
loop also which will execute when the
loop terminates normally i.e. when the
test condition fails in while loop or when
last value is executed in for loop but not
when break terminates the loop
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Syntax of Python loop along with
„else‟ clause
for with “else” while with “else”
for <var> in <seq>: while <test condition>:
statement 1 statement 1
statement 2 statement 2
else: else:
statement(s) statement(s)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example (“else” with while)
i=1
Output
while i<=10: 1
2
print(i) 3
4
i+=1 5
6
else: 7
8
9
print("Loop Over") 10
Loop Over
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example (“else” with for)
names=["allahabad","lucknow","varanasi","kanpur","agra","ghaziabad"
,"mathura","meerut"]
city = input("Enter city to search: ")
for c in names:
if c == city:
print(“City Found")
break Output
else: Enter city to search : varanasi
City Found
print("Not found") Enter city to search : unnao
Not found
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example- program to enter numbers repeatedly and print
their sum. The program ends when the user says no more
to enter(normal termination) or program aborts when the
number entered is less than zero
count = sum = 0
ans = 'y'
while ans=='y':
num = int(input("Enter number :"))
if num < 0:
print("You entered number below zero,
Aborting...")
break
sum += num
count+=1
ans=input("Continue(y/n)")
else:
print("You entered ", count, " numbers so far")
print("Sum of entered numberVINOD
is :",sum)
KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example- program to enter numbers repeatedly and print
their sum. The program ends when the user says no more
to enter(normal termination) or program aborts when the
number entered is less than zero - OUTPUT
Enter number :3 Enter number :10
Continue(y/n)y Continue(y/n)y
Enter number :6 Enter number :20
Continue(y/n)y
Continue(y/n)y
Enter number :50
Enter number :8
Continue(y/n)n
Continue(y/n)y You entered 3 numbers so far
Enter number :-10 Sum of entered number is : 80
You entered number below
zero, Aborting...
Sum of entered number is : 17
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example- program to input number and tests if it is a
prime number or not
num = int(input("Enter any number :"))
lim = num//2 + 1
for i in range(2,lim):
rem = num % i
if rem == 0:
print(num," is not a prime
number ")
break
else:
print(num," is a prime number ")
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example- program to input number and tests if it is a
prime number or not - OUTPUT
Enter any number :23
23 is a prime number
Enter any number :36
23 is not a prime number
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Generating Random numbers
Python allows us to generate random
number between any range
To generate random number we need to
import random library
Syntax: (to generate random number)
◦ [Link](x,y)
◦ it will generate random number between x to
y
◦ For e.g. : [Link](1,10)
◦ Refer to code page no. 156
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Infinite loop and „break‟
We can also execute infinite loop purposely
by specifying an expression which always
remains True.
However in loop we can mention any
condition to exit or terminate loop
a=2
while True:
print(a)
a*=2
if a>100:
break
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Nested Loop (loop within loop)
A loop may contain another loop in its
body. This form of loop is called nested
loop.
In Nested loop, the inner loop will
execute for each value of outer loop.
For e.g. if the outer loop is of 4 times and
inner loop is of 5 times then statement
will execute 4 x 5 = 20 times
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Syntax of Nested Loop
Nested Loop – FOR Nested Loop – WHILE
for i in sequence: while condition:
for j in sequence: while condition:
Statement1 Statement1
Statement2 Statement2
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example – to print table of 2 to 10
4
to
8
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Example – Program to print the following patterns
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]
Write a program to print the following
pyramid pattern
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR