Requesting a destination variable in python

I am writing Fibonacci code in python. The solution below is mine.

enter image description here

While the other solution is below from python.org.

enter image description here

Can someone tell me why it gives a different answer, although the logic for assigning variables is the same?

+4
source share
4 answers

These two programs are not equivalent. The right-hand side of equalities ( =) is evaluated together. Performance:

a=b
b=a+b

Differs from:

a,b = b,a+b

This is actually the same as:

c = a
a = b
b = b + c

Your example is indeed described in the Python documentation :

: a b 0 1. , , . .

+7

a = b # Assigns the value of 'b' to 'a'
b = a + b # Adds the new value of 'a' to 'b'

,

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

+7

, , , a=b , . , b=a+b, a . a . python, , . , . , , .

+5
source

I see additional tabs in your solution, and also the logic of your program is incorrect. As I understand it, writing fib(5), you need the 5th Fibonacci in the series (5), and not a number that is less than 5 (this is 3).

a=b
b=a+b

and

a,b = b,a+b

not equal.

Take a look at the code below.

def fibonacci(num):
    a,b=0,1;
    counter = 2;
    while(a<=):
        a,b = b,a+b
        counter += 1
    return b

print fibonacci(5)
+2
source

Source: https://habr.com/ru/post/1680141/


All Articles