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

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -