nested for

Nested for loop in Python programming language

Nested for loop in Python programming language

We will learn about Nested for loop in Python programming language

Already, we learnt about for loop in python. In python programming language, a for loop inside another for loop is called as nested for loop.

Nested for loop in Python language

Nested for loop has two parts of the loop

  1. Inner for loop
  2. Outer for loop

Outer for loop executes only once when its test expression is true. Then, the flow of control moves to inner loop iteration and inner loop executes until its test expression is false.

When the test expression of the inner loop is false, the flow of control skips the execution of the inner loop and come out to execute the outer loop again.

Declaration

Syntax

The syntax of nested for loop in python

The syntax of nested for loop in Python

 

The flow diagram in nested for loop in Python

When we execute the above program, it will produce the following result

Example

program 1

for i in range(3):
    print("x")
for i in range(4):
    print("y")

 

When we execute the above program, it will produce the following result

x
x
x
y
y
y
y

 

Program 2

for i in range(3):
    print("letter X")
    for i in range(4):
        print("letter Y")

 

When we execute the above program, it will produce the following result

letter X
letter Y
letter Y
letter Y
letter Y
letter X
letter Y
letter Y
letter Y
letter Y
letter X
letter Y
letter Y
letter Y
letter Y

 

Program 3

#nested for loop in python
for i in range(0,3):
    for j in range(0,4):
        print(i,j)

 

When we execute the above program, it will produce the following result

(0, 0)
(0, 1)
(0, 2)
(0, 3)
(1, 0)
(1, 1)
(1, 2)
(1, 3)
(2, 0)
(2, 1)
(2, 2)
(2, 3)

 

Display Floyd’s triangle using Python nested for loop

Program 4

Display Floyd’s triangle

#nested for loop in python example
num=int(input("Enter te number of rows :"))
print(" ")
for i in range(1,num):
    for j in range(1):
        print("*"*(i)), #string multiplication
        print("")

 

When we execute the above program, it will produce the following result

Enter the number of rows: 7

*
**
***
****
*****
******

 

program 5

display multiplication table 1

for i in range(1,11)://#outer for loop
    print(" ")
    for j in range(1,11)://#outer for loop
        print(i*j),
        print("\t"),

 

When we execute the above program, it will produce the following result

1  2  3  4  5  6  7  8  9  10   
2  4  6  8  10  12  14  16  18  20   
3  6  9  12  15  18  21  24  27  30   
4  8  12  16  20  24  28  32  36  40   
5  10  15  20  25  30  35  40  45  50   
6  12  18  24  30  36  42  48  54  60   
7  14  21  28  35  42  49  56  63  70   
8  16  24  32  40  48  56  64  72  80   
9  18  27  36  45  54  63  72  81  90   
10  20  30  40  50  60  70  80  90  100

 

Program 6

display multiplication table 2

for i in range(1,5,1):
    print("\n"),
    for j in range(1,6,1):
        print i,'*',j,'=',i*j,
        print("\n"),

 

When we execute the above program, it will produce the following result

1 * 1 = 1 
1 * 2 = 2 
1 * 3 = 3 
1 * 4 = 4 
1 * 5 = 5 

2 * 1 = 2 
2 * 2 = 4 
2 * 3 = 6 
2 * 4 = 8 
2 * 5 = 10 

3 * 1 = 3 
3 * 2 = 6 
3 * 3 = 9 
3 * 4 = 12 
3 * 5 = 15 

4 * 1 = 4 
4 * 2 = 8 
4 * 3 = 12 
4 * 4 = 16 
4 * 5 = 20

 

Program 7

find the prime number using nested for loop

for var1 in range(2,20):
    bol=True
    for var2 in range(2,var1-1):
        if(var1%var2==0):
            print(var1, "is not prime number")
            bol=False
            break
    if bol:
        print(var1, "is a prime number")
        

 

When we execute the above program, it will produce the following result

(2, 'is a prime number')
(3, 'is a prime number')
(4, 'is not prime number')
(5, 'is a prime number')
(6, 'is not prime number')
(7, 'is a prime number')
(8, 'is not prime number')
(9, 'is not prime number')
(10, 'is not prime number')
(11, 'is a prime number')
(12, 'is not prime number')
(13, 'is a prime number')
(14, 'is not prime number')
(15, 'is not prime number')
(16, 'is not prime number')
(17, 'is a prime number')
(18, 'is not prime number')
(19, 'is a prime number')

 

Nested for loop with multiple lists in Python

We can use nested for loop in multiple lists in Python

Program 1

names=['John','Smith','Dan','Khan']#name is string
//#name list in Python
ages=[34,54,23,43] //#age list in Python, age is int
for name in names: //#outer for loop
    for age in ages: //#inner for loop
        print name," has "+str(age)

 

When we execute the above program, it will produce the following result

John has 34
John has 54
John has 23
John has 43
Smith has 34
Smith has 54
Smith has 23
Smith has 43
Dan has 34
Dan has 54
Dan has 23
Dan has 43
Khan has 34
Khan has 54
Khan has 23
Khan has 43

 

Program 2

names=['Jhon','Smith','Dan']#name is string
#name list in Python
numbers=[1,4,6] #numbere list in Python
cars=['Benz','Toyota','BMW'] #car list in python
for name in names: 
    for number in numbers:
        for car in cars:
          print name+" has "+str(number)+"car"+car

 

When we execute the above program, it will produce the following result

Jhon has 1carBenz
Jhon has 1carToyota
Jhon has 1carBMW
Jhon has 4carBenz
Jhon has 4carToyota
Jhon has 4carBMW
Jhon has 6carBenz
Jhon has 6carToyota
Jhon has 6carBMW
Smith has 1carBenz
Smith has 1carToyota
Smith has 1carBMW
Smith has 4carBenz
Smith has 4carToyota
Smith has 4carBMW
Smith has 6carBenz
Smith has 6carToyota
Smith has 6carBMW
Dan has 1carBenz
Dan has 1carToyota
Dan has 1carBMW
Dan has 4carBenz
Dan has 4carToyota
Dan has 4carBMW
Dan has 6carBenz
Dan has 6carToyota
Dan has 6carBMW

 

 

Suggested for you

For loop in Java language

For loop in C++ language

For loop in C language

For loop in Python language

 

Nested for in Java programming language
Nested for loop in C programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

  • Thank you for another excellent article. Where else could anyone get that type of info in such an ideal way of writing?
    I've a presentation next week, and I am on the look for such information.

Share
Published by
Karmehavannan

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago