Table of Contents
Test your understanding of Python 3 control flow with these 13 multiple-choice questions. Choose one answer for each, then expand its explanation. For output questions, assume the code runs exactly as shown.
if, elif, and else choose a branch; for iterates over an iterable; and while repeats while a condition is true. They are different control structures, not different names for a loop.
Before you begin
Review Python indentation if the blocks are unfamiliar. For loop exits, see break and continue. The final example intentionally shows a nonterminating loop; reason about it rather than leaving it running.
Questions
1. What does this conditional print?
x = 5
if x > 3:
print("A")
else:
print("B")ABBoth A and B
Answer and explanation
A. The condition is true, so the if block runs and the else block is skipped.
2. Which branch runs?
x = 7
if x > 5:
print("first")
elif x > 2:
print("second")
else:
print("third")firstsecondfirst and second
Answer and explanation
A. An if/elif chain selects the first true branch; it does not continue to later branches.
3. How is an empty list evaluated in an if condition?
TrueFalseIt always raises an error
Answer and explanation
B. An empty list is false in a Boolean context. A nonempty list is true.
4. What values are produced by list(range(3))?
[1, 2, 3][0, 1, 2][0, 1, 2, 3]
Answer and explanation
B. The default start is zero, and the stop value is excluded.
5. What is list(range(2, 7, 2))?
[2, 4, 6][2, 4, 6, 7][2, 3, 4, 5, 6]
Answer and explanation
A. The range starts at 2, advances by 2, and stops before 7.
6. What does for item in a_dictionary iterate over by default?
The valuesThe keysKey-value pairs
Answer and explanation
B. Direct dictionary iteration yields keys. Use values() for values or items() for key-value pairs.
7. What is the final value of n?
n = 3
while n > 0:
n -= 1310
Answer and explanation
C. Each iteration subtracts one. When n reaches zero, the condition becomes false.
8. Which numbers are printed, each on its own line?
for n in range(1, 5):
if n == 2:
continue
print(n)1, 3, 411, 2, 3, 4
Answer and explanation
A. continue skips the remainder of the current iteration, so only 2 is omitted.
9. Which numbers are printed?
for n in range(5):
if n == 2:
break
print(n)0, 1, 20, 10, 1, 3, 4
Answer and explanation
B. break exits the loop before print runs for n equal to 2.
10. Does the loop’s else block run here?
for n in range(2):
print(n)
else:
print("done")Yes, after 0 and 1No, because the loop ranOnly if the range is empty
Answer and explanation
A. A for loop’s else runs when iteration finishes without break.
11. What is printed?
for n in range(3):
if n == 1:
break
else:
print("done")
print("after")done and afterafter onlyNothing
Answer and explanation
B. break skips the loop’s else, but execution continues after the loop.
12. What is the final value of count?
count = 0
for row in range(2):
for column in range(3):
count += 1356
Answer and explanation
C. The inner loop runs three times for each of the two outer iterations: 2 × 3 = 6.
13. Why does this loop keep printing 1?
n = 1
while n < 3:
print(n)range() is missingn never changes, so the condition stays trueprint() resets n
Answer and explanation
B. Without a state change or an exit, n remains 1. Adding n += 1 inside the loop would make this example finish after printing 1 and 2.
Use your results to choose the next practice task
Count one point per correct answer, for a score out of 13. A score of 8 is a useful practice target, but inspect the missed topics rather than relying only on the total. Questions 1–3 cover conditions, 4–6 cover iteration, and 7–13 cover loop behavior and exits.
Write a small example for each missed concept and predict its result before running it. For more practice, use the Python exercises with sample solutions. Python’s official control-flow tutorial explains the underlying language rules.
Reader Comments 0
Sign in with email or Google to join the discussion.