+ 6

# Why?

#I think those two(2) code are the same; right? #but why the result goes different? a = 0 b = 1 for i in range(10): print(a) a,b = b,a+b #Line break print(""" """) c = 0 d = 1 for i in range(10): print(c) c = d d = c+d

4 Respostas

+ 4

No it's not right 😉
In the first case, the tuple is unpacked
a = 0
b = 1
for i in range(10):
print(a)
# 1 0 1
a,b = b,a+b
#Line break
print("""
""")
c = 0
d = 1
for i in range(10):
print(c)
c = d
# 1 1
d = c+d
#Line break
print("""
""")
tuple = 0,1
for _ in range(10):
print(tuple[0])
tuple = tuple[1], tuple[0]+tuple[1]

+ 7

In the first one the value of a in a+b is the one before the assignment. The assignments happen at the same time, I'm guessing. (the guess part is: I don't know if I'm allowed to call a, b = b, a+b a tuple, but it works like a tuple, the old values are used)
In the second one, c in c+d is the new c value.
The order of assignment is changed.

+ 7

+ 6

IbrahimCPS
a,b = b,a is a quick pythonic way to swap the values of 2 variables.
As you loop through the first code, the value growth of a is being inhibited because it keeps getting re-assigned the value of b
In your second code, the values of c & d are not swapping back & forth during the iterations, so c can grow at a steady exponential rate