Python Forum
Why are two similar bits of code giving different results?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why are two similar bits of code giving different results?
#1
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!
Reply
#2
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?
Reply
#3
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?
Reply
#4
set b = 2
since a = b, a = 2

now set b = a + b, so b = 2 + 2 or 4
Reply
#5
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?
Reply
#6
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.
Reply
#7
(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.
Reply
#8
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
99 percent of computer problems exists between chair and keyboard.
Reply
#9
(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 ;)
Reply
#10
It will come to you naturally when you come more regularly here ;)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,067 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 722 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,580 Mar-27-2023, 07:38 AM
Last Post: buran
  How to rotate bits ? korenron 2 1,637 Mar-23-2022, 04:05 PM
Last Post: Larz60+
  From list of bits to PDF drimades 1 1,920 Nov-02-2021, 08:55 PM
Last Post: Gribouillis
  Sum similar items tester_V 3 1,982 Jun-29-2021, 06:58 AM
Last Post: tester_V
Exclamation Help in breaking bytes into bits through masking and shifting kamui123 9 4,588 Jan-11-2021, 07:42 AM
Last Post: kamui123
  Code giving same output no matter the input. Yort 2 2,562 Dec-20-2020, 05:59 AM
Last Post: buran
  Compiling Python 3.8.5 source code results in build error Deepan 0 2,192 Sep-14-2020, 04:11 AM
Last Post: Deepan
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,244 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020