I'm given the following program, and I've been asked to write down what it prints:
num = 10
for num in range(5):
print(num)
print(num)
My answer is:
10
My reasoning is that num
has been defined to be 10
, so it can't be in the range (0,5)
, so we can skip this loop. Then, we simply write down what num
is: 10.
The answer is very different:
0
1
2
3
4
4
How on earth is this the case, and how could the last line possibly print 4, when num
has been defined to be 10
at the start?