Assigning multiple variables in one line

后端 未结 3 2018
闹比i
闹比i 2021-01-24 02:11

I am trying to make the Fibonacci sequence I don\'t get why this:

def fibonacci(n):
    f1 = 0
    f2 = 1
    i = 1

    while i < n:
        print(f2)
               


        
3条回答
  •  独厮守ぢ
    2021-01-24 02:44

    The variables f1 and f2 simultaneously get the new values f2 and f1 + f2, the expression

    f1, f2 = f2, f1 + f2.

    Demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. f1 + f2 use the f1 old value we don't use the f1 new value. The right-hand side expressions are evaluated from the left to the right.

提交回复
热议问题