0

Yes i realize this is the fibonacci sequence and i understand the math im just having trouble understanding the line in red what does it equate to in laymans terms?
Thanks in advance :D

a,b=0,1
while b < 100:
  print b
  a,b=b,a+b
3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Celtrix
Featured Replies
  • It's setting a equal to b, and b equal to a+b. Achieved by this as well: a = 0 b = 1 while b < 100: print b a = b b=a+b They just used some short hand statements. This : num1,num2 = 12,72 Is the same thing as : … Read More

  • 1

    Traditional way to do this is by temporary variable, because we need the old value of b in two formulas on the right side. Let's call the temporary variable new_b: [CODE]a,b=0,1 while b < 100: print b new_b = a + b a = b b = new_b [/CODE] alternatively … Read More

1

It's setting a equal to b, and b equal to a+b.

Achieved by this as well:

a = 0
b = 1
while b < 100:
print b
a = b
b=a+b

They just used some short hand statements.

This :

num1,num2 = 12,72

Is the same thing as :

num1 = 12
num2 = 72

Edited by Soup Or Sonic: n/a

0

Hmm it spits out doubles when i do that instead of the fibonacci sequence any ideas?

1

Traditional way to do this is by temporary variable, because we need the old value of b in two formulas on the right side. Let's call the temporary variable new_b:

a,b=0,1
while b < 100:
  print b
  new_b = a + b
  a = b
  b = new_b

alternatively you can become clever and deside that it does not matter to destroy old value of b as the new value is a+b and the old b is a+b-a:

a,b=0,1
while b < 100:
  print b
  b = a + b
  a = b - a

Best way of course is the original Pythonic double assignment, where right hand values have the old variable values before the assignments.

Edited by pyTony: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.