Find the answer to your Linux question:
Results 1 to 3 of 3
>>> # Fibonacci series: ... # the sum of two elements defines the next ... a, b = 0, 1 >>> while b < 10: ... print b ... a, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2004
    Location
    Tokyo
    Posts
    24

    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. #2
    Just Joined!
    Join Date
    Nov 2004
    Location
    Tokyo
    Posts
    24
    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. #3
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    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 "=".
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

Posting Permissions

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