My question concerns the output of this statement:
for x in range(4), y in range(4):
print x
print y
Results in:
[0, 1, 2, 3]
2
True
2
It seems there is a comparison involved, I just can’t figure out why the output is structured like this.
My guess is that you’re running this from an interactive console, and already had
ydefined with a value of 2 (otherwise, you’d getNameError: name 'y' is not defined). That would lead to the output you observed.This is due to
for x in range(4), y in range(4):actually being equivalent to the following when evaluated:which reduces to…
which again reduces to…
This then results in 2 iterations of the
forloop, since it iterates over each element of the tuple:x = [0,1,2,3]x = True.(And of course,
yis still 2.)