Python Forum

Full Version: Why are two similar bits of code giving different results?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi guys,

sorry for my novice question, but I can't really see why it gives a different result...
it's about a Fibonacci code...

a=1
b=1
c=1
while c < 11:
    a, b, c = b, a+b, c+1 #it gives the proper output

a=1
b=1
c=1
while c < 11:
    a=b
    b=a+b
    c=c+1 #this code gives the raising to the power of 2
before I red about it I thought it will give the same value to the variables, but I can't see that why it isn't...
Thanks in advance!
In the second one...
a = b
b = a+b  # because of a's re-binding, this is the same as b=b+b
But in the first, that isn't the case, because you assign both at the same time, and thus b's new value still uses a's original binding. Does that make sense?
Thanks for your fast answer, you made it more clear!

So is it because they are in the same line? But how could they receive another value, being a same ones? Huh  like a=b and b=a+b? Which comes first?
Does they even change a value, or just after the next cycle?
set b = 2
since a = b, a = 2

now set b = a + b, so b = 2 + 2 or 4
Sorry, I should reply more specific...

I understand that second example, when everything is in a new line, I have problem understanding the following:

a, b, c = b, a+b, c+1
So is it because they are in the same line? But how could they receive another value, being a same ones? [Image: huh.png]  like a=b and b=a+b? Which comes first?

Does they even change a value, or just after the next cycle?
The right side of the equal sign is evaluated first.  So, to python, everything on the same line turns into roughly
a, b, c = b, a+b, c+1

# is roughly the same as:
old_a = a
old_b = b
old_c = c
a = old_b
b = old_a + old_b
c = old_c + 1
Depending on how it's easier for you to understand, you can think of all three variables being assigned at the same time, so b get's a+b before a has a new value yet.  Or, you can think of it like a bunch of temporary variables are created.  Either way you think of it, the left side of the equal sign has nothing to do with what's happening on the right.
(Dec-08-2016, 11:01 AM)godmode Wrote: [ -> ]Which comes first?
Does they even change a value, or just after the next cycle?

No single value comes first. First all three values on the right side are calculated, only then the original values are replaced. So, yes just after the line, all values are changed at the same time. This is an easier way to write it without storing extra variables. Consider this:

a, b = b, a
When you want to split it in multiple lines, you need a third value c. Try it, maybe it's easier.
Antoher way to look at it. It just unpacking the values from a tuple.
a, b, c = 1, 1, 1
pytuple = (a, a+b, c+1)
a, b, c = pytuple

print a, b, c, pytuple
(Dec-08-2016, 10:11 PM)nilamo Wrote: [ -> ]
a, b, c = b, a+b, c+1

# is roughly the same as:
old_a = a
old_b = b
old_c = c
a = old_b
b = old_a + old_b
c = old_c + 1
Thanks for all of you, this example made it the clearest for me :) Don't know where to find the like button, but I would give you one ;)
It will come to you naturally when you come more regularly here ;)
Pages: 1 2