The documentation website for Python uses this example of Fibonacci – https://docs.python.org/3.6/tutorial/introduction.html:
>>> a, b = 0, 1 >>> while b < 1000: ... print(b, end=',') ... a, b = b, a+b ... 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
I’m quite an experienced programmer, and I found this difficult to digest even though it’s rather simple. The multiple assignment lines threw me off a bit.
In Java and similar languages, you would do this:
int x = 5, y = 7, z = 2;
to assign multiple values. So, each value gets its assignment immediately. In python this is not the case.
In python, you note all the target variables in a list, then you note all the values. So, a more clear example would be:
x, y, z = 5, 7, 2
This would provide the same assignments as the Java example does. This seems quirky but maybe I’m just too used to C-style languages :).
So, the initial python example, ‘a’ starts as 0, ‘b’ starts as 1, and every cycle ‘a’ = ‘b’ and b = ‘a’ + ‘b’, which makes sense.