Answer: Python Puzzler #1037

The answer is D. The program prints:

[9, 12, 15]
What the program prints
Checked by running the program for real.

Why?

append adds one item to the end of the list each time round the loop. The loop runs for n = 3, 4, 5.

Trace table

A trace table follows the program step by step. Here’s this one:

nn * 3results so far
39[9]
412[9, 12]
515[9, 12, 15]

The code again

results = []
for n in range(3, 6):
    results.append(n * 3)
print(results)

Try it: change one number in the program and predict the new output before you run it.