Short code and Fibonacci sequence

I found an example of a Fibonacci sequence that looks like this:

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

fib(20)

So here is what I don't understand:

a, b = 0, 1 # is just a shortcut for writing
a = 0
b = 1

right?

Now following the same logic

a, b = b, a+b #should be the same as writing
a = b
b = a+b

But this is not because if I write it like this, the solution will be different. It’s hard for me to understand why. Any suggestions?

+4
source share
3 answers

Yes. It's not exactly the same, because when you write -

a, b = b, a+b

a b , , a=1, b=2, , b=2 a+b=3. , a 2, b 3.

-

a = b
b = a+b

, b=2, a, a 2, a + b ( a), a+b=4 b, b 4, , , .

a,b = b, a

a b, , , .

, , , sid , , , -

>>> a = 5
>>> b = 10
>>> t = a, b
>>> t
(5, 10)
>>> b, a = t
+3

a, b = c, d :

a = c
b = d

:

a, b = (c, d)

.. (c, d), c d, a, b. , . * , . , .

* "" ACID , .

+1

.

x, y = y, x

:

t = x
x = y
y = t

x y.

, a, b = b, a+b. :

m = a; n = b

a = n

b = m + n

+1

All Articles