3

Python docs:

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

>>> fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

My reimplementation:

>>> def fib2(n):
    a=0
    b=1
    while a<n:
        print a
        a=b
        b=a+b

>>> fib2(2000)
0
1
2
4
8
16
32
64
128
256
512
1024
Aditya Pan
  • 77
  • 11
  • It would be easier to implement it as a recursive function like this: http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python – Krimson Dec 07 '14 at 18:06
  • But won't making it recursive for a huge n make the program slower than the pyth docs implementation? – Aditya Pan Dec 07 '14 at 18:12
  • @Aditya_Pan I doubt that it would became slower. But a potential problem for a large n would be it would run out of stack space and throw a stackOverFlow error. Doing it recursively is best suited for a functional programming language. Its better to do it iteratively in python – Krimson Dec 07 '14 at 18:21

4 Answers4

2

The difference between

a,b=b,a+b

and

a=b
b=a+b

is that in the second one, a is assigned value of b, and then b is assigned the sum of a and b, which means it is twice its original value.

Consider:

a = 1
b = 2
a,b = b,a+b

This gives

a==2 (old value of b)
b==3 (sum of 1 and 2)

Contrarily:

a = 1
b = 2
a = b
b = a + b

which gives:

a==2 (old value of b)
b==a+b==2+2==4 (twice the old value of b)
khelwood
  • 52,115
  • 13
  • 74
  • 94
  • Thanks, I am new to python and that was a really helpful answer, till I have done to far, python is so different than Java! – Aditya Pan Dec 07 '14 at 18:04
1

The difference is that, when you do:

a,b=b,a+b
#     ^

the a that I marked is the original value of a, not the updated value. This is because Python always evaluates what is on the right of the = sign before it evaluates what is on the left.

When you do this however:

a=b
b=a+b

the a in the second line is the new value of a that was assigned in the line above. This causes your calculations to be off.

For more information, here is a reference on assignment statements in Python.


To get the same behavior as the function in the Python docs, you would need a temporary variable to save the original value of a:

tmp=a
a=b
b=tmp+b

Below is a demonstration:

>>> def fib2(n):
...     a=0
...     b=1
...     while a<n:
...         print a, # The comma causes everything to be printed on one line
...         tmp=a
...         a=b
...         b=tmp+b
...
>>> fib2(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
>>>

Of course, doing just:

a,b=b,a+b

is a lot more pythonic. Although I would recommend you put some spaces:

a, b = b, a+b

Readability is everything in Python.

  • Thanks, I am new to python and that was a really helpful answer, till I have done to far, python is so different than Java! – Aditya Pan Dec 07 '14 at 18:05
0
def fib2(n):
a=0
b=1
while a<n:
    print a
    a=b
    b=a+b

Here when you do a = b and then b=a+b This is equal to b = b+b This is not what you want

Now this is what you want :

def fib2(n):
a=0
b=1
while a<n:
    print a
    tmp = b
    b = a+b
    a = tmp

But a,b=b,a+b this is lot more pythonic

Alfie
  • 2,658
  • 1
  • 13
  • 26
0

I believe it's a detail with the comma syntax. Try:

c=b
b=b+a
a=c
narthur157
  • 864
  • 1
  • 8
  • 17