0

I'm a newbie to Python and just can't understand this: Code#1:

a, b = 0, 1
while a < 10:
    print(a)
    a, b = b, a+b

Code#2

a, b = 0, 1
while a < 10:
    print(a)
    a = b
    b = a+b

Why Code#1 produce Fibonacci sequence while Code#2 doesn't?

reza moradi
  • 17
  • 1
  • 5
  • 1
    You've set `a = b` you've lost the value of `a` when you do `b = a+b` which really is `b = b + b`. You'd normally have to use a third, intermediate variable in other languages, but python has multiple assignment/iterable unpacking syntax to do it without an extra variable. – juanpa.arrivillaga Apr 10 '20 at 08:33
  • Yeah. I've added `c = a + b` and `b = c` and It worked! Tnx; – reza moradi Apr 10 '20 at 08:45

0 Answers0