Python - Why does y increase in value after every full for loop iteration?

Viewed 71

Apologies for the rather cryptic question, I couldn't find out how to express it better. But the question is really simple once you see my example.

>>> x,y = 0,0
>>> for x in range(x-1, x+2):
...     for y in range(y-1, y+2):
...             print(x,y)
...
-1 -1
-1 0
-1 1
0 0
0 1
0 2
1 1
1 2
1 3

My question is why y increases by 1 every time x increases by 1? I was expecting the following result:

>>> for x in range(-1,2):
...     for y in range(-1, 2):
...             print(x,y)
...
-1 -1
-1 0
-1 1
0 -1
0 0
0 1
1 -1
1 0
1 1

where y is -1, 0, 1 every iteration.

Can anyone explain why this behaviour is observed?

Thanks!

5 Answers

Simply try out some dummy code like this:

i = 0

for i in range(0, 5):
    print(i)
    
print(i)

Notice that the output is:

0
1
2
3
4
4

This means that, after the loop, the value of i did not reset to its original value (and it had no reason to do so in the first place).

Going back to your question, after the first iteration of the loop, y is updated to the value of y+1. This means that y is being incremented for every iteration of the loop. y would never go back to 0 because, well, you haven't told it to.

Your problem is that you're using the same variable name on your for loops. When you do for y in range(y-1, y+2), you're reassigning the y variable. This means that, once your second for loop ends, y is now actually y+1. To solve this, you can change your variables in your for loop, as such:

>>> x,y = 0,0
>>> for i in range(x-1, x+2):
...     for j in range(y-1, y+2):
...             print(i,j)
...

You can change i and j to any values you'd want, except for x and y.

In the first iteration of the outer loop, for x in range(x-1, x+2):, the loop iterator x takes values from range(0-1, 0+2), that's using the predefined x = 0. The same applies to the first iteration of the inner loop, for j in range(y-1, y+2):. That's why the first 3 outputs are correct.

From the second iteration of the outer loop, however, the last value for y = 1, so this value is used in the range of the new iterations of the inner loop, and so on.

Iterations run like this:

x = -1  y = -1
        y =  0
        y =  1  # last y
x =  0  y =  0  # use last y in range(y-1, y+2)
        y =  1
        y =  2  # last y
x =  1  y =  1  # use last y in range(y-1, y+2)
        y =  2
        y =  3

For loops change the value of the variable you are iterating in the range. The first for loop will only change the value of x, and the second loop will change the value of y. So, it will update the value of y at the end of every loop increasing it by one. To display the y variable as you want it, you should declare it after creating the first for loop:

x = 0
for x in range(x-1, x+2):
    y = 0
    for y in range(y-1, y+2):
        print(x,y)

This happens because the variable y is never resetted after every y-for-loop cycle.

First cycle:

x=-1  y=-1
x=-1  y=0
x=-1  y=1

The the X changes from -1 to 0 but y is not resetted and y=1! And you get that the next y-for-loop starts from y=1.

To achieve what you are looking for you need to reset the y variable after every y-for-loop cycle:

x,y = 0,0
for x in range(x-1, x+2):
    y=0
    for y in range(y-1, y+2):
        print(x,y)

Hope that helped!

Related