Enjoy an ad free experience by logging in. Not a member yet? Register.
1. Python While Calculation Question

>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
... print b
... a, b = b, a+b
...
1
1
2
3
5
8

Ok, I'm just not getting the logic/calculation here, in python's while. When the while 1st initiates, a=0 and b=1 so 1 is printed, then a becomes b(1) and b should(?) become a+b(1+1), no..? So, why is 1 printed a second time..? Cheers.

2. Ok, I get it....
... a, b = b, a+b hasn't been interpreted yet, so, a+b can only be 0+1 again, and then it is 1+2 next time round, yes..? woo hoo...bloody logical, really...lol..

3. Good More exactly, python computes b, a+b (without assigning it to anything yet), then packs those two values into a tuple, next unpacks the values to the variables on the left of "=".

4. \$spacer_open \$spacer_close

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•