Re: beginner question fibonacci

2005-07-17 Thread Robert Kern
Joon wrote:
 
   # 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
 
   a, b = 0, 1
   while b  10:
   print b
   a = b
   b = a+b
   
 1
 2
 4
 8
 
 Why a, b = b, a+b isn't a = b; b = a+b ?

It's actually equivalent to:

temp = (b, a+b)
a = temp[0]
b = temp[1]

The temporary tuple object is created first, with the old values of a 
and b. Then a and b are reassigned. The value of a doesn't change until 
*after* a+b is calculated.

-- 
Robert Kern
[EMAIL PROTECTED]

In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die.
   -- Richard Harter

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: beginner question fibonacci

2005-07-17 Thread Michael Hoffman
Joon wrote:

   a, b = 0, 1
   while b  10:
 print b
 a = b
 b = a+b
 
 
 1
 2
 4
 8
 
 Why a, b = b, a+b isn't a = b; b = a+b ?

Because you changed a before you added it to b.

Let's call your existing a and b a0 and b0, and the next a and b 
a1 and b1. When you do a, b = b, a+b you are assigning:

a1 = b0
b1 = a0 + b0

But when you use separate statements, you are assigning:

a1 = b0
b1 = a1 + b0 = b0 + b0 = 2*b0
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: beginner question fibonacci

2005-07-17 Thread ralobao
The case is that Python in attribution commands solves first the right
side, so he atributes the vars.

So the a+b expression is executed first.

Joon escreveu:
  # 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



   a, b = 0, 1
   while b  10:
   print b
   a = b
   b = a+b
 
   
 1
 2
 4
 8
 
 Why a, b = b, a+b isn't a = b; b = a+b ?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: beginner question fibonacci

2005-07-17 Thread Joon
Yes, i see.
Thank you very much for the fast help!
-- 
http://mail.python.org/mailman/listinfo/python-list