Answer: Python Puzzler #940

The answer is B. The program prints:

[9, 16]
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.

Trace table

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

nn * nresults so far
39[9]
416[9, 16]

The code again

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

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