#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

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.

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

Screenshots