amp-web-push-widget button.amp-subscribe { display: inline-flex; align-items: center; border-radius: 5px; border: 0; box-sizing: border-box; margin: 0; padding: 10px 15px; cursor: pointer; outline: none; font-size: 15px; font-weight: 500; background: #4A90E2; margin-top: 7px; color: white; box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.5); -webkit-tap-highlight-color: rgba(0, 0, 0, 0); } .amp-logo amp-img{width:190px} .amp-menu input{display:none;}.amp-menu li.menu-item-has-children ul{display:none;}.amp-menu li{position:relative;display:block;}.amp-menu > li a{display:block;} /* Inline styles */ figure.acss855b8{max-width:2480px;}figure.acssa59de{max-width:368px;}div.acss138d7{clear:both;}div.acssf5b84{--relposth-columns:3;--relposth-columns_m:2;--relposth-columns_t:2;}div.acssc61bb{aspect-ratio:1/1;background:transparent url(https://code4coding.com/wp-content/plugins/related-posts-thumbnails/img/default.png) no-repeat scroll 0% 0%;height:150px;max-width:150px;}div.acss6bdea{color:#333333;font-family:Arial;font-size:12px;height:75px;}div.acss8d0ac{aspect-ratio:1/1;background:transparent url(https://code4coding.com/wp-content/uploads/2018/12/whileloop1.jpg) no-repeat scroll 0% 0%;height:150px;max-width:150px;}div.acss8e1fb{aspect-ratio:1/1;background:transparent url(https://code4coding.com/wp-content/uploads/2018/08/floyd3-150x150.jpg) no-repeat scroll 0% 0%;height:150px;max-width:150px;}a.acss9bfd5{font-size:14.52427184466pt;}a.acssc37f8{font-size:16.427184466019pt;}a.acss29e97{font-size:16.631067961165pt;}a.acss361c8{font-size:17.174757281553pt;}a.acss51c7b{font-size:20.029126213592pt;}a.acssa2e10{font-size:20.097087378641pt;}a.acss5dd67{font-size:21.728155339806pt;}a.acssf0e8e{font-size:12.077669902913pt;}a.acss759e3{font-size:17.922330097087pt;}a.acss0abf8{font-size:21.252427184466pt;}a.acss6bf84{font-size:13.504854368932pt;}a.acss349b0{font-size:10.038834951456pt;}a.acssf23c5{font-size:8pt;}a.acss7e0a8{font-size:9.2233009708738pt;}a.acsse6f77{font-size:16.970873786408pt;}a.acssc51bb{font-size:14.116504854369pt;}a.acss38f57{font-size:11.26213592233pt;}a.acss066f0{font-size:22pt;}a.acss4e811{font-size:17.31067961165pt;}a.acss9cc90{font-size:12.417475728155pt;}a.acss01721{font-size:15.339805825243pt;}a.acsse9f66{font-size:15.543689320388pt;}a.acss72254{font-size:20.708737864078pt;}a.acsseedeb{font-size:20.776699029126pt;}a.acss25b87{font-size:14.320388349515pt;}a.acss7c517{font-size:12.757281553398pt;}a.acss7a3ee{font-size:18.941747572816pt;}a.acssf92d5{font-size:18.26213592233pt;}a.acss551d3{font-size:16.291262135922pt;} .icon-widgets:before {content: "\e1bd";}.icon-search:before {content: "\e8b6";}.icon-shopping-cart:after {content: "\e8cc";}
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 programming languageNested for loop in Python programming language
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

Nested for loop in Python programming languageNested for loop in Python programming language
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

 

 

Similar post

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

New keyword in Java language

For loop in Java language

While loop in Java language

Nested for loop in Java language

Nested while loop in Java language

 

Suggested for you

Two dim Array in C++ language

Three dim Array in C++ language

Single dim Array in Java language

Two dim Array in Java language

Three dim Array in Java language

Single dim Array in C language

Two dim Array in C language

Three dim Array in C 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

10 best Ways to Subtract Two Numbers in Java (With Examples)

10 best Ways to Subtract Two Numbers in Java (With Examples) In this article, we…

3 weeks ago

Array Data Structure: Definition, Types, Operations & Advantages

Array Data Structure: Definition, Types, Operations & Advantages Array Data Structure Introduction In this post,…

4 weeks ago

20 ways to subtract two numbers in Java

20 ways to subtract two numbers in Java In this article, we will discuss the…

4 weeks ago

10 simple ways to add two numbers in Java

10 simple ways to add two numbers in Java In this article, we will discuss…

2 months ago

Write a Python program to find the first n prime numbers

Write a Python program to find the first n prime numbers In this article we…

2 months ago

Python: Calculate Average of odd and even in a list using loops

Python: Calculate Average of odd and even in a list using loops In this post,…

3 months ago