Multiple assignment and evaluation order in Python

前端 未结 10 1909
醉酒成梦
醉酒成梦 2020-11-22 01:41

What is the difference between the following Python expressions:

# First:

x,y = y,x+y

# Second:

x = y
y = x+y

First gives diffe

10条回答
  •  盖世英雄少女心
    2020-11-22 02:20

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

    Output

    1
    1
    2
    3
    5
    8
    

    the variables a and b simultaneously get the new values 0 and 1, the same a, b = b, a+b, a and b are assigned simultaneously.

提交回复
热议问题