Answer key · 12 questions

Python For Loops Exercises With Answers

All 12 questions for Python For Loops in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

Which values does range(3) produce?

  1. 0, 1, 2correct
  2. 0, 1, 2, 3
  3. 1, 2, 3
  4. 3

Answer: 0, 1, 2

Why: range(stop) starts at 0 and excludes stop.

02 · Multiple choice

What does the loop variable receive in for order in orders?

  1. A copy of the whole list
  2. Only the index
  3. The length of orders
  4. Each value in orderscorrect

Answer: Each value in orders

Why: A for loop assigns each item from the iterable to the loop variable.

03 · Multiple choice

Where should a numeric accumulator be created?

  1. Inside the loop body
  2. After printing the total
  3. Inside the print call
  4. Before the loopcorrect

Answer: Before the loop

Why: The accumulator must exist before the first update and survive between iterations.

04 · Multiple choice

What does the program output?

print(list(range(2, 5)))
  1. [2, 3]
  2. 2, 3, 4
  3. [2, 3, 4]correct
  4. [0, 1, 2]

Answer: [2, 3, 4]

Why: range(2, 5) starts at 2 and stops before 5.

05 · Fill in the blank

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.

06 · Fill in the blank

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.

07 · Fill in the blank

Complete the update so total grows by price each time.

for price in prices:
    total ____ price

Answer: +=

Why: total += price is shorthand for total = total + price.

08 · Fill in the blank

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.

09 · Predict output

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.

10 · Predict output

What is printed?

total = 0
for n in [2, 4, 6]:
    total += n
print(total)

Answer: 12

Why: The loop accumulates 2 + 4 + 6.

11 · Predict output

What is the output?

print([x * 2 for x in [1, 2, 3]])

Answer: [2, 4, 6]

Why: A list comprehension doubles every item.

12 · Predict output

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.

Practice this lesson