01110100
01110100

Reputation: 833

Why does this code only work when i use 'x,y = y,x + y format instead of 'x = y; y = x + y'?

I am working on Project euler, that's beside the point though. I am writing a quick code to print out the Fibonacci sequence. I actually have a different code for the project eueler equation, but this is essentially the algorithm I am using:

x = 1
y = 0
while x < 4000000:
    print x
    x = y
    y = x + y

This was erking me for awhile, this should have worked. I then researched a bit and found virtually the same code but in a tad bit different of a format. And it worked! The only difference was it put the x and y assignments into a single line, separated by a comma. So I tried it:

x , y = 1 , 0
while x < 4000000:
    print x
    x,y = y, x + y

Obviously, as I said, it worked. This is really bothering me as I can't figure out what the difference is between the two other than being clever and using less lines in the second one. I cant understand why the output is different. Why is the output different?

Upvotes: 0

Views: 109

Answers (5)

Rohit Jain
Rohit Jain

Reputation: 213351

In your first code:

x = y
y = x + y

You are actually assigning y + y to y. Since the value of x is already overwritten. And this is not you wanted right?


And in your 2nd code:

x, y = y, x + y

First y and x + y on the RHS is evaluated, and then the evaluated value is assigned to x, y on the LHS. So, x + y will not have any side-effect of newly assigned value to x, as was happening in the first case. So, your y will only have x + y.

So, it's just the case of evaluation of both those expression on RHS, before the actual assignment is done.

And yes, the assignments outside your while loop, will not make any difference. But the 2nd way looks more appealing.

Upvotes: 6

dparpyani
dparpyani

Reputation: 2503

That's because after the line x = y, the second line (y = x + y) is evaluated as y = y + y. For example, let's say x = 2 and y = 3.

Then,

x = y          # i.e. x = 3 now
y = x + y      # i.e. y = 3 + 3 = 6 (while you wanted it to be 5)

On the other hand,

x, y = y, x + y  # this first assigns values to the right hand side

So,

x, y = 3, 2 + 3  # i.e x, y = 3, 5

Upvotes: 0

Kylotan
Kylotan

Reputation: 18449

In the first version, the first assignment changes the value of x. This affects the value in the assignment on the next line. This means y = x + y is more like y = y+y because you already changed x.

In the second version, the two assignments are done simultaneously, so both values are updated simultaneously.

Upvotes: 0

Hyperboreus
Hyperboreus

Reputation: 32449

You reassign x.

Let's take a numeric example

Case 1:

x = 4
y = 2
x = y = 4
y = x + y = 4 + 4 = 8

Case 2:

x = 4
y = 2
x, y = y, x + y = 2, 4 + 2 = 2, 6

Hence in the first case x is 4 and y is 8, while in the second case x is 2 and y is 6.

Upvotes: 0

thisisdog
thisisdog

Reputation: 948

I would guess that in the second version, when it evaluates y, x+y it uses the original value of x in the x+y.

In the two line version, when it evaluates x+y x has been set to y which leads to x+y being the same as y+y

Upvotes: 0

Related Questions