0
0
Pythonprogramming~5 mins

Nested for loop execution in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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)
A3
B5
C2
D6
In nested loops, which loop runs first?
AOuter loop
BInner loop
CBoth run at the same time
DDepends on the code
What will this code print? for i in range(1, 3): for j in range(1, 2): print(i, j)
A(1, 1) and (2, 1)
B(1, 1) only
C(2, 1) only
DNo output
What is the total number of iterations for nested loops with outer loop range 4 and inner loop range 5?
A9
B5
C20
D10
Which of these is a correct nested for loop syntax in Python?
Afor i in range(3): for j in range(2): print(i, j)
Bfor i in range(3): for j in range(2): print(i, j)
Cfor i in range(3): for j in range(2): print(i, j)
Dfor i in range(3) for j in range(2): print(i, j)
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.