What is nested loops in python?

Description

To see how to implement nested loop using python3.

   Use loop within the loop.

   Elif conditions.

   Else conditions.

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)

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit