I apologize since this might be a common question, but I think I am looking for a quite specific answer that wouldn’t be found in other topics. Basically, I am quite confused about the flow of adding numbers. Here are two similar codes that compute numbers differently. Is there any simple explanation for this?
>>> a = 0
>>> b = 1
>>> while b <1000:
print b
a, b = b, a+b
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987
>>> a =0
>>> b=1
>>> while b<1000:
print b
a = b
b = a+b
1,2,4,8,16,32,64,128,256,512
The difference lies in what the values are WHEN swapped
a, b = b, a+bsets a to b and sets a to a+b but the swaps are done relatively at the same time so it’s not in order, ie the change in b doesn’t respect that a was changed first.
In the second example
the values are changed and the 2nd statement respects the change of the first