Python >>>  a, b = b, a + b

>>> a , b = 0 , 1
>>> a
0
>>> b
1

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

      
1
1
2
3
5
8

Is it the same as the following codes?

>>> a , b = 0 , 1
>>> a
0
>>> b
1


>>> while b < 10:
      print(b)
      a = b
      b = a + b

      
1
2
4
8


The answer is "NO" because the expressions on the right hand side are evaluated before being assigned to the left hand side. 
Thus
it is equivalent to:

>>> a , b = 0 , 1
>>> a
0
>>> b
1

>>> while b < 10:
      print(b)
      c = a + b
      a = b
      b = c

      
1
1
2
3
5
8

     


[Reference]
https://docs.python.org/3/tutorial/introduction.html
http://stackoverflow.com/questions/21990883/python-a-b-b-a-b


 

arrow
arrow
    文章標籤
    Python
    全站熱搜

    DanBrother 發表在 痞客邦 留言(1) 人氣()