How Can We Help?
This tutorial will explain how to use Python Nested Loops. It is one type of Python loop statement with basic syntax by example for better understanding.
What is a nested loop in Python? A nested loop in Python is when the outer loop contains an inner nested loop. We can put any loop inside another kind of loop or the same type of loop like a for loop inside a while loop, or a while loop inside a for loop, or a for loop inside a for loop, or a while loop inside a while loop.
Python Nested For Loop Syntax
for <iterating_var> in <sequence1>:
for <iterating_var> in <sequence2>:
Body of for loop2
Body of for loop1
Python Nested For Loop Example
#loop i
for i in range(1,3):
# loop j
for j in range(i):
print("j",end=' ')
print("i\n",end='')
Output
j i
j j i
In the above Example, loop ‘i’ takes i=(1,2) and loop ‘j’ takes j=(0,1) in respective iteration cycle. ” print(“j”,end=’ ‘)” is a print statement of loop ‘j’ and “print(“i\n”,end=”)” is a print statement of loop ‘i’. For first iteration of loop’ i’ is i=1, j=0 so print statement of’ ‘j’ and then ‘i’ is executed and returns first line of patterns as ‘j i‘.
For second iteration of loop’ i’ is i=2, j= (0,1) so first iteration of loop ‘j’ is j=0 so print statement of ‘j’ is executed and return ‘j’, then second iteration of loop ‘j’ is j=1, then ‘i’ is executed and returns ‘ j i’. Combining all iteration output, ‘ j j i’, which shows the second line of output pattern.
Python Nested While Loop Syntax
while <condition1>:
while <condition2>:
Body of while loop2
Body of while loop1
Python Nested While Loop Example
i=2
while(i>0): # loop i
j=2
while(j>i): # loop j
print("j",end=' ')
j-=1
i-=1
print("i\n",end='')
Output
I
j i
In the above example, for the first iteration of the loop ‘i’ is i=2 which is greater than ‘0’ and j=2, which is not satisfying the condition “j>i”, so loop ‘j’ is stopped and prints statement as ” print(“i\n”,end=”)” of the loop ‘i’ is executed as a next line after loop ‘j’ and returns the value as ‘i’ which is first output.
After the first iteration, i=2-1=1, which is greater than ‘0’ and j=2, which is greater than i=1, so condition is satisfied, and prints statement as a print(“j”,end=’ ‘) of loop ‘j’ is executed and return ‘j’ then after the print state of the loop ‘i’ is executed and return ‘i’. Combine these two outputs as ‘j i’, the second output.
Python Loop Inside Loop Inside Loop Syntax
loop N:
loopN-1:
loopN-2:
loopN-2 body
|
|
|
loopN-1 body
loopN body
Python Loop Inside Loop Inside Loop Example
# loop i
for i in range(1,4):
# loop j
for j in range(i):
# loop k
for k in range(j):
print("k",end=' ')
print("j",end=' ')
print("i\n",end='')
Output
j i
j k j i
j k j k k j i
IIn the above Example, loop ‘i’ takes i=(1,2,3), loop ‘j’ takes j=(0,1,2) and loop k takes k=(0,1) value in respective iteration cycle. ” print(“k”,end=’ ‘)” is a print statement of loop ‘k’, ” print(“j”,end=’ ‘)” is a print statement of loop ‘j’ and “print(“i\n”,end=”)” is a print statement of loop ‘i’. For first iteration of loop’ i’ is i=1, j=0 so ‘k’ has no range so only print statement of’ ‘j’ and ‘i’ is executed and returns first line of patterns as ‘j i‘.
For second iteration of loop’ i’ is i=2, j= (0,1) so first iteration of loop ‘j’ is j=0 so ‘k’ has no range so print statement of ‘j’ is executed and return ‘j’ then second iteration of loop ‘j’ is j=1 so k=0 which executes the print statement of ‘k’ then ‘j’ then ‘i’ and returns ‘k j i’. Combine all iteration output ‘j k j i’, which shows the second line of the output pattern.
For third iteration of loop ‘i’ is i=3, j= (0,1,2) so first iteration of loop ‘j’ is j=0 so ‘k’ has no range so print statement of ‘j’ is executed and return ‘j’ then second iteration of loop ‘j’ is j=1 so k=0 which executes the print statement of ‘k’ then ‘j’ and returns ‘k j ‘ then last iteration j=2 so k=(0,1)
So the first iteration of loop ‘k’ is k=0 which executes the print statement of ‘k’ and return ‘k’ and the second iteration of loop ‘k’ is k=1 which executes the print statement of ‘k’ then ‘j’ then ‘i’ and returns ‘k j i’. Combine all iteration output, ‘ j k j k k j i’, and show the third line of output pattern.
Conclusion
In this tutorial, you have learned how to use nested loop in Python, you also learned how to use nested while loop and we have explained how to use nested for loop.
Hopefully, it was clear and concise.
If you have an inquiry or doubt don’t hesitate to leave them in the comment section. we are waiting for your feedback.
Related Articles
Python Loop Statements – Comprehensive Guide
Python Tutorials List:
- Python tutorials list : access all python tutorials.