0

Guys I'm trying to print the first 8 elements of the Fibonacci sequence and just want to understand one piece of code. When performing an assignment operation in a single line (n1,n2 = n2, n1+ n2) the code works fine, but when splitting the same assignment operation into separete lines like (n2 = n1+n2 and n1=n2), I get different results and code doesn't work in a way it supposed to, please explain me why?

def generate_fibonacci():
    n1 = 0
    n2 = 1
    while True:
        yield n1
        n1,n2 = n2, n1+ n2 // this piece of code doesn't work properly when separating it
        # n2 = n1 + n2
        # n1 = n2
        
seq = generate_fibonacci()

for i in range(8):
    print(next(seq))
Zohrab
  • 3
  • 3
  • If you split that line, then the part that you do second uses the *new* value of the variable in the first part, rather than the old value. – jasonharper Jan 19 '22 at 17:08
  • When you separate the lines, the n2's latest value(n1+n2) is taken, and assigned to n1 – Anand Sowmithiran Jan 19 '22 at 17:08
  • `n1,n2 = n2, n1+ n2` Using this code, `n1` is assigned to the _original_ value of `n2`. – John Gordon Jan 19 '22 at 17:08
  • Does this answer your question? [Multiple assignment and evaluation order in Python](https://stackoverflow.com/questions/8725673/multiple-assignment-and-evaluation-order-in-python) – Tomerikoo Jan 19 '22 at 23:12

3 Answers3

0

If you change n2 before n1, then n1 changes to the new value of n2 not the original value, so they both have the same value.

Similarly, changing n1 first would make the value of n2 incorrect.

If you want to split the code then there are two options. The first works in any case and is to store the original value of one variable in another variable:

temp = n2
n2 = n1 + n2
n1 = temp

The second option only works in this case and is to compute the be value of n1 taking into account the fact that the value of n2 had changed:

n2 = n1 + n2
n1 = n2 - n1
Lecdi
  • 2,189
  • 2
  • 6
  • 20
  • 1
    There is no way to split it ? Are there any official Python references regarding to these kind of special situations – Zohrab Jan 19 '22 at 17:17
  • I've updated the answer to include some possible ways to split up the code. – Lecdi Jan 19 '22 at 17:38
0
n2 = n1 + n2
n1 = n2

Using this code, n1 and n2 both end up with the same value -- the sum of the original n1 and n2.

n1,n2 = n2, n1+ n2

Using this code, n1 ends up with the original value of n2, and n2 ends up with the sum of the original n1 and n2.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

When you do n1,n2 = n2,n1+n2, Python stores the values on the right in temporary memory before performing the assignments. So the n1+n2 is computed before n1 is altered (thus avoiding interference during the assignment process).

If you do it in two lines, you lose the original value of one of the variables. But, because of the nature of the fibonacci sequence, you can compensate for that and retrieve the original value:

def generate_fibonacci():
    n1 = 0
    n2 = 1
    while True:
        yield n1
        n2=n1+n2  # n2 gets a new value
        n1=n2-n1  # recover original value of n2
Alain T.
  • 40,517
  • 4
  • 31
  • 51