# Nested Loops in Python

Spread the love

It is possible to write one loop inside another loop. For example we can write a for loop inside a while loop or a for loop inside another for loop. Such loops are called Nested Loops.

Take the following for loop –

for i in range(3): # i values are from 0 to 2
for j in range(4): # j values are from 0 to 3
print('i=', i, '\t', 'j=', j) # display i and j values

The outer for loop repeats for 3 times by changing i values from 0 to 2. The inner for loop repeats for 4 times by changing j values from 0 to 3. Observe the indentation (4 spaces) before the inner for loop. The indentation represents that the inner for loop is inside the outer loop. Similarly, print() function is inside the inner for loop.

When the outer for loop is executed once, the inner for loop is executed for 4 times. It means when i value is 0, j values will change from 0 to 3. So print() function will display the following output.

i= 0 	 j= 0
i= 0 	 j= 1
i= 0 	 j= 2
i= 0 	 j= 3

Once the inner for loop execution is completed (j reached 3) then python interpreter will go back to outer for loop to repeat the execution for second time. This time, i value will be 1. Again the inner for loop is executed for 4 times. Hence print() function will display the following output.

i= 1 	 j= 0
i= 1 	 j= 1
i= 1 	 j= 2
i= 1 	 j= 3

Since the inner for loop execution is completed, again the interpreter will go back to outer for loop. This time i value will be 2 and the output will be.

i= 2 	 j= 0
i= 2 	 j= 1
i= 2 	 j= 2
i= 2 	 j= 3

In this way, the print() function in the inner for loop is executed for 12 times. if we observe the output given above, we can understand that the outer for loop is executed for 3 times and the inner for loop is executed for 4 times. Therefore the print() function in the inner for loop is executed for 3 * 4 = 12 times.

Let’s write a python program to display stars ( *s) in a right angled triangular form.

for i in range(1, 11): # to display 10 rows
for j in range(1, i+1): # no. of stars = row number
print('* ', end='')
print()
#output
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * 

In the first row, there is only one star. In the second row there are two stars. In the third row there are three stars. It means

Row number = number of starts in that row.

Since there are 10 rows, we can write an outer for loop that repeats for 10 times from 1 to 10 as

for i in range(1, 11): # to display 10 rows

To represent the columns (or stars) in each row, we can write an inner for loop. This for loop should repeat as many times as the row number indicated by the outer for loop. Suppose row number is i then the inner for loop should repeat for 1 to i times. Hence the inner for loop can be written as

for j in range(1, i+1): # repeat for 1 to i times

In the above program observe the following print() statement

print('* ', end='')

This will display the start symbol. The end=” represents that it should not throw the cursor to the next line after display each star. So all the stars are displayed in the same line. But we need to throw the cursor into the next line when there is a new row starting i.e. when i value changes. This is achieved by another print() statement in the outer for loop that does not display anything but simply throws the cursor into the next line.

Rating: 1 out of 5.