Python cycle

  1. >>> a = 0
  2. >>> b = 1
  3. >>> a
  4. 0
  5. >>> b
  6. 1
  7. >>> while b <10:
  8. ... print b
  9. ... b = a+b
  10. ... a = b
  11. ...
  12. 1
  13. 1
  14. 2
  15. 4
  16. 8
  1. >>> a = 0
  2. >>> b = 1
  3. >>> while b <10:
  4. ... print b
  5. ... a, b = b, a+b
  6. ...
  7. 1
  8. 1
  9. 2
  10. 3
  11. 5
  12. 8
Why are written on one line and not written in a line of different results.?
How do I feel the first is right?

Started by Betty at February 07, 2016 - 12:28 AM

  1. a, b = b, a+b
Should be equivalent to:
  1. a = b
  2. b= a+b
.?
This and you:
  1. b = a+b
  2. a = b
Obviously:
Not the same order.
While the output is not the same as the results,
That's normal.?

Posted by Donald at February 17, 2016 - 12:38 AM

If you write is the Fibonacci sequence, then the first algorithm is wrong.
Here a should be equal to the "last" B, you first algorithm, a is equal to "the last" b value.
Equivalent to f(0) = 0, f(1) = 1, f(n) = f(n-1)+ f(n-1).
And your second algorithm is correct, f(n) = f(n-1)+ f(n-2)

Posted by Borg at December 19, 2016 - 8:31 PM

  1. b = a+b
  2. a = b
  3. The above... I feel that it is equivalent to a = b = a + B
  4. a, b = b, A+b is equivalent to:
  5. c = a
  6. a = b
  7. b = c + b

Posted by Susan at December 25, 2016 - 8:43 PM

  1. >>> a = 0
  2. >>> b = 1
  3. >>> while b <10:
  4. ... print b
  5. ... a = b
  6. ... b = a+b
  7. ...
  8. 1
  9. 2
  10. 4
  11. 8
It seems not order problems, the outcome is the same. .

Posted by Betty at December 28, 2016 - 9:04 PM

Posted from your results, you can see:
  1. 1
  2. 1
  3. 2
  4. 4
  5. 8

And:
  1. 1
  2. 2
  3. 4
  4. 8

As a result, can be called:
Same
? ? ?

Of course, if you want to discuss your:
What is the 1.
The 2 algorithm is wrong
That is another matter.
We need to make sure you:
1 local how, want to achieve what effect, what algorithm
2 and then to discuss, the code you write, is wrong, should be changed
The.
What do you say?

Posted by Donald at January 01, 2017 - 9:54 PM

  1. a, b = b, a+b
  2. Equivalent to
  3. A = the original B
  4. B = the original a + original B
While
  1. a = b
  2. b = a + b
  3. In second, a value changes, in fact, are equal to
  4. b = b + b

Posted by Borg at January 03, 2017 - 11:59 PM

Feel shy, the result is not the same. Thank you!

The intention was to get Fibonacci series results, thought
  1. a, b = b, a+b
  2. And
  3. a = b
  4. b = a+b
  5. Is the same
Thank you!

Posted by Betty at January 09, 2017 - 10:51 PM

Thank you very much!

See in the python document
  1. >>> a, b = 0, 1
  2. >>> while b <10:
  3. ... print b
  4. ... a, b = b, a+b
  5. ...
  6. 1
  7. 1
  8. 2
  9. 3
  10. 5
  11. 8
  1. The first line contains composite parameters: variable a and B at the same time is set to 0 and 1. The last line of this is used again, proved that the expressions on the right-hand side of the assignment before the first operation. On the right side of the expression from left to right operation.
The first terms should be used in the original A and the original B phase.

Thank you!

Posted by Betty at January 12, 2017 - 10:53 PM

Personally feel that this is the correct explanation, look at the books:

a,b = b,A+b this is a tuple assignment, simply omit the()
Tuple assignment Python will establish a temporary tuple, to store the original value of the variable

Posted by Megan at January 13, 2017 - 11:52 PM