• 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
• pro@slogix.in
• +91- 81240 01111

### What is nested loops in python?

###### Description

To see how to implement nested loop using python3.

###### Process

Use loop within the loop.

Elif conditions.

Else conditions.

###### Sample Code

Source code (check odd or even):

#get input from user

n=int(input(“Enter the number:”))

#initialize the loop with condition

if(n>0):

#another loop with in the loop

if(n%2==0):

print(“Given number is even”)

#nested loop

elif(n%2!=0):

print(“Given number is odd”)

print()

Source code

(Find prime numbers in a interval):

#get the lower limit

l=int(input(“Enter the lower limit:”))

#get the upper limit

u=int(input(“Enter the upper limit:”))

#initialize the loop conditions

for i in range(l,u+1):

#another if loop within for loop

if (i>1):

#another loop

for j in range(2,i):

if (i%j==0):

break

#else statement

else:

print(i)