What is Nested For Loop in Python?

         A nested for loop is a loop inside another loop. The outer loop runs first, and for each time it runs, the inner loop runs completely. This way, you can repeat actions within repeated actions.

For example, think of it as going through rows and columns: for each row, you go through all the columns.

In simple terms:

  • Outer loop: Runs multiple times.
  • Inner loop: Runs inside the outer loop for each time the outer loop runs.

You can have more than two loops nested inside each other. For example, you can have a loop inside a loop, and then another loop inside that, and so on. Each inner loop will run fully before the outer loop moves to the next step.

It's like going through several layers of tasks:

  • First, complete the innermost task,
  • Then move up to the next outer layer,
  • And keep repeating this process.

Syntex

for first_veriable in first_sequence:
    for second_veriable in second_sequence:
        #our block of code

Let's take an example

a = [1, 2, 3, 4]
b = [100, 200]

for i in a:
    print(i)
    
    for j in b:
        print(j)

Let's Understand This:

  • The first loop starts, and when it gets an element from list a, it moves inside.
  • Then, the second loop starts and runs until it finishes all elements of list b.
  • After the second loop finishes, the first loop takes the next element from its list (a), goes inside again, and the second loop runs completely with that new element.
  • This process continues until the first loop finishes going through all its elements.

Output:

1
100
200
2
100
200
3
100
200
4
100
200

=== Code Execution Successful ===

In this example:

  • a is a list with values [1, 2, 3, 4].
  • b is a list with values [100, 200].

The for loop goes through each element of list a. For each element of a, it runs another for loop to go through each element of list b.

So, for every item in a, it prints the item, and then prints every item from b. This process repeats for each item in a.

Output:

  1. Prints 1 and then 100 and 200.
  2. Prints 2 and then 100 and 200, and so on.

 

Why do we use I and J for for loops?

       We use i and j in loops because they are short and easy to type. i is often used for the first loop, and j is used for the second loop if we have two loops inside each other. It's a habit from math and programming, but you can use any name you like instead of i or j. They are just common choices to keep things simple.

Problem: Create tables from 1 to 5 using nested for loop in Python?

for x in range(1, 6):
    for y in range(1, 11):
        print(f"{x} x {y} = {x*y}")
    print() #only use for space after complete a table

Output

1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50


=== Code Execution Successful ===

 

Leave a comment

You must be logged in to post a comment.

0 Comments