Bird
Raised Fist0
Pythonprogramming~20 mins

Nested for loop execution in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Nested Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested loops with range
What is the output of this Python code?
Python
result = []
for i in range(2):
    for j in range(3):
        result.append((i, j))
print(result)
A[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]
B[(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2)]
C[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
D[(0, 0), (1, 1), (2, 2), (3, 3)]
Attempts:
2 left
💡 Hint
Think about how the inner loop runs completely for each step of the outer loop.
Predict Output
intermediate
2:00remaining
Sum of products in nested loops
What is the value of variable total after running this code?
Python
total = 0
for x in range(1, 4):
    for y in range(1, 3):
        total += x * y
print(total)
A18
B20
C24
D12
Attempts:
2 left
💡 Hint
Calculate the sum of x*y for x=1 to 3 and y=1 to 2.
🔧 Debug
advanced
2:00remaining
Identify the error in nested loops
What error does this code raise when run?
Python
for i in range(3):
    for j in range(2):
        print(i, j)
ASyntaxError
BTypeError
CIndentationError
DNo error, prints pairs
Attempts:
2 left
💡 Hint
Check the syntax of the for loops carefully.
Predict Output
advanced
2:00remaining
Output of nested loops with break
What is the output of this code?
Python
result = []
for a in range(3):
    for b in range(3):
        if b == 1:
            break
        result.append((a, b))
print(result)
A[(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)]
B[(0, 0), (1, 0), (2, 0)]
C[(0, 0), (0, 1), (1, 0), (2, 0)]
D[(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)]
Attempts:
2 left
💡 Hint
The break stops the inner loop when b equals 1.
🧠 Conceptual
expert
2:00remaining
Number of iterations in nested loops
How many times will the innermost statement print(i, j, k) execute in this code?
Python
for i in range(2):
    for j in range(3):
        for k in range(4):
            print(i, j, k)
A6
B9
C12
D24
Attempts:
2 left
💡 Hint
Multiply the lengths of all ranges to find total iterations.

Practice

(1/5)
1. What does a nested for loop do in Python?
for i in range(2):
for j in range(3):
print(i, j)
easy
A. Runs the inner loop fully for each outer loop step
B. Runs both loops only once
C. Runs the outer loop fully for each inner loop step
D. Runs only the outer loop

Solution

  1. Step 1: Understand nested loop structure

    The outer loop runs 2 times (i=0,1). For each i, the inner loop runs 3 times (j=0,1,2).
  2. Step 2: Observe loop execution order

    For each i, the inner loop completes all its iterations before the outer loop moves to next i.
  3. Final Answer:

    Runs the inner loop fully for each outer loop step -> Option A
  4. Quick Check:

    Inner loop runs fully per outer loop [OK]
Hint: Inner loop runs completely inside each outer loop step [OK]
Common Mistakes:
  • Thinking outer loop runs inside inner loop
  • Assuming loops run only once
  • Confusing loop order
2. Which of the following is the correct syntax for a nested for loop in Python?
easy
A. for i in range(3): for j in range(2): print(i, j)
B. for i in range(3): for j in range(2): print(i, j)
C. for i in range(3): for j in range(2): print(i, j)
D. for i in range(3): for j in range(2): print(i, j)

Solution

  1. Step 1: Check indentation rules

    Python requires consistent indentation to define nested blocks. Inner loop must be indented inside outer loop.
  2. Step 2: Identify correct indentation

    for i in range(3): for j in range(2): print(i, j) uses 4 spaces indentation for inner loop and print statement, which is correct.
  3. Final Answer:

    for i in range(3):\n for j in range(2):\n print(i, j) -> Option B
  4. Quick Check:

    Proper indentation = for i in range(3): for j in range(2): print(i, j) [OK]
Hint: Indent inner loop inside outer loop with spaces [OK]
Common Mistakes:
  • Missing indentation for inner loop
  • Incorrect indentation for print
  • Writing loops on same line without colon
3. What is the output of this code?
for i in range(2):
    for j in range(2):
        print(i + j, end=' ')
    print()
medium
A. 0 1 \n0 1
B. 1 2 \n2 3
C. 0 1 1 2
D. 0 1 \n1 2

Solution

  1. Step 1: Calculate inner loop sums for i=0

    j=0: 0+0=0, j=1: 0+1=1 -> prints '0 1 '
  2. Step 2: Calculate inner loop sums for i=1

    j=0: 1+0=1, j=1: 1+1=2 -> prints '1 2 '
  3. Final Answer:

    0 1 \n1 2 -> Option D
  4. Quick Check:

    Sum of i and j per line = 0 1 \n1 2 [OK]
Hint: Add i and j for each inner loop iteration [OK]
Common Mistakes:
  • Ignoring print() creates new line
  • Adding values incorrectly
  • Confusing end=' ' usage
4. Find the error in this nested loop code:
for i in range(3):
for j in range(2):
    print(i, j)
medium
A. print statement should be outside loops
B. Missing colon after inner for loop
C. Inner loop not indented inside outer loop
D. range(2) should be range(3)

Solution

  1. Step 1: Check indentation of inner loop

    Inner for loop must be indented inside outer loop to be nested properly.
  2. Step 2: Identify correct indentation

    Here, inner loop is at same level as outer loop, causing IndentationError.
  3. Final Answer:

    Inner loop not indented inside outer loop -> Option C
  4. Quick Check:

    Indent inner loop inside outer loop [OK]
Hint: Indent inner loop under outer loop to fix error [OK]
Common Mistakes:
  • Not indenting inner loop
  • Misplacing colons
  • Wrong range values
5. How many times will the print statement execute in this nested loop?
for i in range(4):
    for j in range(3):
        for k in range(2):
            print(i, j, k)
hard
A. 24
B. 12
C. 36
D. 9

Solution

  1. Step 1: Calculate iterations of each loop

    Outer loop: 4 times (i=0..3), middle loop: 3 times (j=0..2), inner loop: 2 times (k=0..1).
  2. Step 2: Multiply iterations for total prints

    Total = 4 * 3 * 2 = 24 print executions.
  3. Step 3: Re-check options

    36 says 36, but calculation is 24, so correct is 24.
  4. Final Answer:

    24 -> Option A
  5. Quick Check:

    4*3*2 = 24 prints [OK]
Hint: Multiply all loop ranges for total executions [OK]
Common Mistakes:
  • Adding instead of multiplying loop counts
  • Counting only outer loops
  • Misreading loop ranges