What is the output of the following nested control structure in Python when executed?
for x in range(3):
for y in range(x):
print x,y
I know the answer is
1 0
2 0
2 1
But it is not clear for me why it is this output.
I know that the range(3) function would give you {0, 1, 2} so why is not the first output 0 0 instead of 1 0?
Because range(0) returns an empty list [], so the inner loop does nothing the first time it is run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With