+ 1

Hello. Could you please explain me the difference..

def fibonacci(n):# x = 0 y = 1 s = [] for i in " " * n: s += [x]; x, y = y, x + y return s def fibonacci_(n):# x = 0 y = 1 s = [] for i in " " * n: s += [x] x = y y = x + y return s print(*fibonacci(10)) #0,1,1,2,3,5,8,13,21,34. print(*fibonacci_(10)) #0 1 2 4 8 16 32 64 128 256 Why doesn't the "fibonacci_" function return the same as the "fibonacci" function?

4th Feb 2022, 7:02 PM
Alvaro Becerra
Alvaro Becerra - avatar
1 Answer
+ 2
In fibonacci_() the value of x is changed prior to being added to y and then assigned back as the value of y. fibonacci() the current values are substituted in-line before being assigned. Example: x = 2 y = 3 x, y = y, x + y This would translate to; x, y = 3, 2 + 3 So that x would now have the value of 3, while y would now have the value of 5. While; x = y y = x + y Would translate to; x = 3 y = 3 + 3 Here, x would get the value 3 and y would evaluate to 6.
4th Feb 2022, 7:43 PM
ChaoticDawg
ChaoticDawg - avatar