-1

I like to browse stackovorflow for gems of python code. I came across this tidbit yesterday, and it has me baffled.

Can someone explain how the following code works, what sort of code it is, and where I can find more information on using it?

I have tried searching for this sort of thing, but not knowing what sort of animal it is, the searches have returned nothing useful.

# Fibonacci Series

n = 1000
a, b=0, 1
while a<n:
    print(a,end=" ")
    a, b=b, a+b
print()

The lines that have me baffled are "a, b=0, 1" and "a, b=b, a+b"

They seem to be a format for some sort of default data type, but the type() command chokes and dies on it. Tampering with the structure can send it into an endless loop or cause the program to crash. Ditto with trying to split the parts up into separate lines, or replacing the commas with semi-colons.

Is it some form of python comprehension?

I love this stuff! Thanks for any insight you can offer.

Barmar
  • 741,623
  • 53
  • 500
  • 612
user10637953
  • 370
  • 1
  • 10
  • Is it easier to understand with different spacing? `a,b = b,(a+b)` – Barmar Oct 04 '19 at 18:39
  • If you search in your browser for "Python multiple assignment", you'll find references that can explain this much better than we can manage here. – Prune Oct 04 '19 at 18:40
  • Fair enough.The bit about b=b is really bothersome. It seems a pointless truism, but if it is changed, the program goes bonkers. No matter. I will do the search you suggest, Prune. I am sure it makes sense when understood in the right way. Thank you all. – user10637953 Oct 04 '19 at 18:44
  • This exact code can be found in dozens of other SO questions. Search for `[python] fibonacci multiple assignment` – Barmar Oct 04 '19 at 18:46
  • There is no `b = b` here ! –  Oct 04 '19 at 18:47
  • I agree my question is a duplicate to the one G. Anderson gives a link to, and I will mark it so. My thanks to everyone who offered help and advice. – user10637953 Oct 04 '19 at 18:55

3 Answers3

1

This is a tuple assignment. I guess rearranging spaces and adding parenthesis will make it more clear:

a, b=0, 1 is (a, b) = (0, 1)

and

a, b=b, a+b is (a, b) = (b, a+b)

Marat
  • 15,215
  • 2
  • 39
  • 48
  • I believe it is even clearer if you use explicit parentheses, e.g. `(a, b) = (b, a+b)` – whydoubt Oct 04 '19 at 18:44
  • @whydoubt agree. Will update the answer – Marat Oct 04 '19 at 18:45
  • Yes, I think it is a key point that the original *formatting* was unclear, even potentially misleading, to a person not familiar with tuple assignment. With the better formatting, while they might not fully understand it, they are far less likely to *mis*-understand it. – whydoubt Oct 04 '19 at 19:01
1

The example you mentioned is from the Python documentation.

a, b = 0, 1 means a = 0 and b = 1.

While a, b = b, a + b means a = b and b = a + b.

It's important to note that b = a + b uses the previous value of a, because when you make multiple assignments in the same line it works as if they were executed at the same time (even though in practice they aren't).

Telmo Trooper
  • 4,993
  • 1
  • 30
  • 35
1

This is the multiple assignment syntax. Read (a, b)= (b, a+b). All right expressions are evaluated before the assignments on the left.

Handy for swaps: a, b= b, a or permutations, among others: a, b, c= b, c, a.


Note that a, b= b, a+b is not equivalent to a= b followed by b= a+b nor conversely.