I am testing a python script, where I have to constantly store the old values of 2 variables and then return them through a function. For this I took fibonacci series as an example:
def fibonacci(n):
a=0;b=1;cnt=0;
while True:
if (cnt > n):
return;
yield a;
c=b;b=a+b;a=c;
cnt +=1;
fib=fibonacci(5)
for x in fib:
print(x,end=" ")
Everything works perfectly as expected here, and the output is also as expected. I however tried to write it in a different way, where instead of using an extra variable "c", I can still do the swap of old values efficiently, and I practically bumped on this (though have no idea how the assignment is working here) :
Instead of the line:
c=b;b=a+b;a=c;
Used:
a,b=b,a+b
So a detailed explanation will be appreciated.