Nested Loop in Python refers to a ‘loop within loop’.
In simple words, we can say that nested loops define the idea of loops inside a loop. It might have 2 or more loops.
Syntax
for i in list1:
for j in list2:
while(conditions):
Statements
Statements
Statements
First loop in hierarchy is termed as outer loop and others are termed as inner loops.
Nested loops are generally used in 2D arrays or list implementation and in various sorting algorithms like bubble sort, insertion sort, selection sort etc.
To understand the working of Nested loops, let’s us see the worldwide famous pattern program
*
* *
* * *
* * * *
* * * * *
* * * * * *
'''
*
* *
* * *
* * * *
* * * * *
* * * * * *
'''
for row in range(0,6):
for col in range(0, row+1):
print("*", end=" ")
print()
O/P :
*
* *
* * *
* * * *
* * * * *
* * * * * *
Above program is just the proof of ‘Awesomeness of Nested loops‘. That’s all, see you in the next lecture.