Recall & Review
beginner
What is a nested for loop?
A nested for loop is a loop inside another loop. The inner loop runs completely for each step of the outer loop.
Click to reveal answer
beginner
How many times does the inner loop run if the outer loop runs 3 times and the inner loop runs 4 times?
The inner loop runs 4 times for each of the 3 outer loop steps, so 3 × 4 = 12 times in total.
Click to reveal answer
beginner
What is the order of execution in nested for loops?
The outer loop runs one step, then the inner loop runs all its steps. This repeats until the outer loop finishes.
Click to reveal answer
beginner
Write a simple nested for loop that prints pairs of numbers from 1 to 2.
for i in range(1, 3):
for j in range(1, 3):
print(f"({i}, {j})")
Click to reveal answer
beginner
Why are nested loops useful?
Nested loops help when you want to work with pairs or groups of items, like rows and columns in a table.
Click to reveal answer
How many times will the inner loop run in this code?
for i in range(2):
for j in range(3):
print(i, j)
✗ Incorrect
The outer loop runs 2 times, and for each time the inner loop runs 3 times, so 2 × 3 = 6 times.
In nested loops, which loop runs first?
✗ Incorrect
The outer loop runs first, then the inner loop runs completely for each step of the outer loop.
What will this code print?
for i in range(1, 3):
for j in range(1, 2):
print(i, j)
✗ Incorrect
The outer loop runs for i=1 and i=2, inner loop runs once for j=1 each time, so prints (1, 1) and (2, 1).
What is the total number of iterations for nested loops with outer loop range 4 and inner loop range 5?
✗ Incorrect
Total iterations = outer loop count × inner loop count = 4 × 5 = 20.
Which of these is a correct nested for loop syntax in Python?
✗ Incorrect
Python requires indentation for nested loops. Option B shows correct indentation.
Explain how nested for loops work and why they are useful.
Think about how you count rows and columns in a table.
You got /3 concepts.
Write a nested for loop that prints all pairs of numbers from 1 to 3.
Use range(1, 4) for both loops and print inside inner loop.
You got /3 concepts.