A python nested loop is loop with in a loop. Python programming allows you to create such loops. Now the choice of loops is totally up to the programmer. You can for-loop
inside for-loop, for-loop
inside while-loop, while-loop
inside a while
and so on.Therefore, various combination of loop is possible with deep levels of nesting.
In this article, we will discuss about 4 different types of nesting which are
- for-loop within for-loop
- for-loop within while-loop
- while-loop within while-loop
Note that all kind of loop are interchangeable. A while loop
can do the task performed by a for-loop
or for-each loop
.
For-Loop Within For-Loop
In this example, we are going to print a number triangle. Since, we already know that the number of iterations are fixed. The best choice would be for-loops.
for i in range(1,11): for j in range(1,i + 1): print(j, end=" ")
In the example above the loop for variable i will run from 1 to 10. For each value of i , the variable j will run up to i + 1.
Note that if range(1, 11)
, then python will print 1
to 10
, not 11
. Therefore, range(1, i + 1)
will include all numbers from 1
to i
leaving last number alone
In the program above, i
represents the rows and j
represents the columns. therefore, for each i(row)
there would be j
columns. See the output below.
=== RESTART: C:/Python_projects/Loops/For_loop_inside_For_loop.py ======= 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 >>>
For-Loop Within While-Loop
In our next example, we will modify the previous program using a for-loop
nested inside a while-loop
.
i = 1 while i < 11: for j in range(1, i + 1): print(j, end= " ") i = i + 1
The code is very similar to previous one except that now we are using an outer while loop which initializes a variable i
, puts termination condition i < 11
and finally increment it for each iteration i = i + 1
. The above code will produce similar output as we saw earlier.
== RESTART: C:/Python_projects/Loops/for-loop_inside_while_nested_loops.py == 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 >>>
While-Loop Within While-Loop
In this example, we will be using nested while-loop. This will work exactly like two for-loops.
i = 0 while i < 10: j = 0 while j < i: print(j, end=" ") j = j+ 1 i = i + 1 print()
The output of the program is given below. This time we have started with 0 and ends in 9.
===== RESTART: C:/Python_projects/Loops/Nested_While_loops.py ======= 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5 0 1 2 3 4 5 6 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 9 >>>
Scope of Variable Inside Loop
The scope of the variable is not changing. If the variable is created inside of the loop, then it can be seen as long as you enter the loop at least once. Otherwise, the local variable will be treated as unknown variable and gives Name error.
# Program to print sum of numbers count = 0 for i in range(10): sum = 10 sum = sum + i; count = count + 1 print(sum)
The program above will print the correct value of sum as long as the loop is executed. The variable sum will be created, however, if we do not enter the loop, it will never create sum and return an error while printing.
If you create a global variable like count
in the above program. You can use it in any function regardless of whether the loop is entered or not. It will never give a Name
error.