0
0
Pythonprogramming~10 mins

Nested for loop execution in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested for loop execution
Start outer loop i=0
Start inner loop j=0
Execute inner loop body
Update inner loop j=j+1
Check inner loop j < limit?
NoEnd inner loop
Yes
Update outer loop i=i+1
Check outer loop i < limit?
NoEnd outer loop
Back to inner loop body
The outer loop starts and for each iteration, the inner loop runs completely before the outer loop moves to the next iteration.
Execution Sample
Python
for i in range(2):
    for j in range(3):
        print(i, j)
Print pairs of i and j where i goes from 0 to 1 and j goes from 0 to 2 for each i.
Execution Table
StepijCondition i < 2Condition j < 3ActionOutput
100TrueTruePrint (0,0)(0, 0)
201TrueTruePrint (0,1)(0, 1)
302TrueTruePrint (0,2)(0, 2)
403TrueFalseInner loop ends
510TrueTruePrint (1,0)(1, 0)
611TrueTruePrint (1,1)(1, 1)
712TrueTruePrint (1,2)(1, 2)
813TrueFalseInner loop ends
920FalseN/AOuter loop ends
💡 Outer loop i=2 is not less than 2, so the loop ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
iN/A00001112
jN/A01230123
Key Moments - 3 Insights
Why does j reset to 0 after the inner loop ends?
Because the inner loop starts fresh for each new i in the outer loop, as shown in steps 4 to 5 in the execution_table where j goes from 3 (end) back to 0.
Why does the outer loop increment only after the inner loop finishes?
The outer loop waits for the inner loop to complete all its iterations before increasing i, as seen between steps 4 and 5 where inner loop ends before i increments.
What happens when the inner loop condition j < 3 becomes false?
The inner loop stops running and control returns to the outer loop to increment i, as shown in steps 4 and 8 where j=3 causes inner loop to end.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of j at step 6?
A2
B0
C1
D3
💡 Hint
Check the 'j' column in execution_table row for step 6.
At which step does the outer loop condition become false?
AStep 9
BStep 8
CStep 4
DStep 3
💡 Hint
Look at the 'Condition i < 2' column in execution_table to find when it is False.
If the inner loop range changed to 2, how many total print outputs would there be?
A3
B4
C6
D2
💡 Hint
Multiply outer loop count (2) by new inner loop count (2) to find total prints.
Concept Snapshot
Nested for loops run one loop inside another.
Outer loop runs first, then inner loop runs fully each time.
Syntax: for i in range(...): for j in range(...):
Inner loop resets each outer loop iteration.
Useful for grids, tables, or combinations.
Full Transcript
This visual execution shows how nested for loops work in Python. The outer loop variable i starts at 0 and runs while less than 2. For each i, the inner loop variable j runs from 0 to 2. Each step prints the current i and j values. When j reaches 3, the inner loop ends and i increments. When i reaches 2, the outer loop ends. Variables i and j change as shown in the variable tracker. Key moments explain why j resets and how loops end. The quiz tests understanding of variable values and loop conditions.