This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python For Loops, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
Which values does range(3) produce?
Answer: 0, 1, 2
Why: range(stop) starts at 0 and excludes stop.
What does the loop variable receive in for order in orders?
Answer: Each value in orders
Why: A for loop assigns each item from the iterable to the loop variable.
Where should a numeric accumulator be created?
Answer: Before the loop
Why: The accumulator must exist before the first update and survive between iterations.
What does the program output?
print(list(range(2, 5)))Answer: [2, 3, 4]
Why: range(2, 5) starts at 2 and stops before 5.
Complete the range so the loop visits 0, 1, and 2.
for i in range(____):
print(i)Answer: 3
Why: range(3) produces 0, 1, and 2.
Complete the loop so it visits each item in orders.
orders = ["latte", "mocha"]
for order in ____:
print(order)Answer: orders
Why: The iterable is placed after in.
Complete the update so total grows by price each time.
for price in prices:
total ____ priceAnswer: +=
Why: total += price is shorthand for total = total + price.
Complete the step so range produces 0, 2, and 4.
print(list(range(0, 6, ____)))Answer: 2
Why: The third argument to range is the step size.
What appears on the screen?
print(list(range(3)))Answer: [0, 1, 2]
Why: range(3) produces 0, 1, 2; list shows them in brackets.
What is printed?
total = 0
for n in [2, 4, 6]:
total += n
print(total)Answer: 12
Why: The loop accumulates 2 + 4 + 6.
What is the output?
print([x * 2 for x in [1, 2, 3]])Answer: [2, 4, 6]
Why: A list comprehension doubles every item.
What does this code print?
letters = []
for ch in "abc":
letters.append(ch)
print("".join(letters))Answer: abc
Why: The loop collects the characters and join turns them back into one string.
Try the questions first
Open the practice page for Python For Loops and answer the same 12 questions with instant feedback.