Python a, b = b, a +b

前端 未结 7 2117
独厮守ぢ
独厮守ぢ 2020-11-28 07:24

This is my first question and I started to learn Python. Is there a difference between:

a, b = b, a + b

and

a = b
b = a +          


        
相关标签:
7条回答
  • 2020-11-28 08:26

    In a, b = b, a + b, the expressions on the right hand side are evaluated before being assigned to the left hand side. So it is equivalent to:

    c = a + b
    a = b
    b = c
    

    In the second example, the value of a has already been changed by the time b = a + b is run. Hence, the result is different.

    0 讨论(0)
提交回复
热议问题