python - Assigning multiple variables in one line -
i trying make fibonacci sequence don't why this:
def fibonacci(n): f1 = 0 f2 = 1 = 1 while < n: print(f2) f1 = f2 f2 = f1 + f2 += 1 return f3 returns 1, 2, 4, 8, while this:
def fibonacci(n): f1 = 0 f2 = 1 = 1 while < n: print(f2) f1, f2 = f2, f1 + f2 += 1 return f3 returns fibonacci sequence.
in latter example, right hand side evaluated first:
f1, f2 = f2, f1 + f2 so value of f1 used in calculation of f2 "old" value.
in code, when do:
f1 = f2 f2 = f1 + f2 the value of f1 has changed when go evaluate new value f2.
Comments
Post a Comment