Question About Fibonacci Seq

0 Halcyon Abraham Ramirez · June 27, 2015

 So this is the Fibonacci sequence.

I hope bucky or someone could explain this because its really odd

a = b


def fib(n): 
    a, b = 0, 1 
    while b < n: 
        print(b)   
a, b = b, a+b 
   
fib(4)
    

this outputs:
0
1
1
2


now if we tried doing this:

def fib(n): 
    a, b = 0, 1 
    while b < n: 
        print(b)   
a = b
b = a+b 
   
fib(4)

this outputs:

0
1

I mean doesnt:

a,b = b,a+b

just equate to?
a = b
b = a+b




can someone please explain?

Post a Reply

Replies

Oldest  Newest  Rating
0 RIAS GREMORY · June 30, 2015
Thanks man i appreciate that.
0 RIAS GREMORY · June 30, 2015
I just thought that it was simple math,

In the first case the computer took 4 possibilities i.e. (0,0),(0,1),(1,0)and (1,1).

while in the second case when it took 0 or 1  it ended up in only two possibilities because of condition.
0 Halcyon Abraham Ramirez · June 30, 2015
tbh. Im not really good in math XD thats why i didn't ge it. but i do now :D a lil bit I guess
0 RIAS GREMORY · June 30, 2015
try putting the value '1' in both the equations, the first equation satisfies and gives Fibonacci series but the second one doesn't satisfy it.

Just like maths.  
0 Halcyon Abraham Ramirez · June 30, 2015
yeah.

but what was confusing was

a,b = b , a+b
is just the same as

a = b
b = a + b

however during that multiple assignment in the fibonacci script
the output was altered.


if your interested here is a better explaination here  http://stackoverflow.com/questions/31118687/shorthand-code-and-fibonnaci-sequence
  • 1

Python

106,976 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator