Answer: Python Puzzler #582

The answer is B. The program prints:

melon
What the program prints
Checked by running the program for real.

Why?

A negative position counts from the end: -1 is the last item, -2 the one before it. So -4 means item number 4 from the end.

Trace table

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

positionnegative positionitem
0-5apple
1-4melon
2-3pear
3-2grape
4-1mango

The code again

basket = ["apple", "melon", "pear", "grape", "mango"]
print(basket[-4])

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