-4

Seems the same code but i get the different results

# code1

a = 0
b = 1
for i in range(0, 10):
    print(a)
    a = b
    b = a + b
# code2

a, b = 0, 1
for i in range(0, 10):
    print(a)
    a,b = b, a + b
a = 0
b = 1
for i in range(0, 10):
    print(a)
    a = b
    b = a + b

print()

a, b = 0, 1
for i in range(0, 10):
    print(a)
    a,b = b, a + b

I expected the same output

rdas
  • 20,604
  • 6
  • 33
  • 46
Xme
  • 1

1 Answers1

0

When you use the code 1 : first, a takes the value of b and then b become a+b, but with the new value of a.

In code 2, the evaluation of a and b is "simultaneous", as you use unpacking. In the same time, a takeb and b takes the value of a+b, but a still have his initial value.

Hope that I'm clear !

Rowin
  • 435
  • 2
  • 9