Python Language Question?
Benjamin Kaplan
benjamin.kaplan at case.edu
Sun Feb 27 17:58:22 CET 2011
On Sun, Feb 27, 2011 at 11:45 AM, Paul Symonds <Paul.J.Symonds at gmail.com> wrote:
> Can someone give and explanation of what is happening with the following:
>
>>>> a,b = 0,1 # this assigns a = 0 and b = 1
>
>>>> while b < 10:
>
> ... print b
> ... a, b = b, a+b
> ...
> 1
> 1
> 2
> 3
> 5
> 8
>
>
>>>> a=0
>>>> b=1
>>>> while b < 1000:
>
> ... print b
> ... a = b
> ... b = a+b
> ...
> 1
> 2
> 4
> 8
> 16
> 32
> 64
> 128
> 256
> 512
>
>
> Why is this statement .. a, b = b, a+b
Python evaluates the entire right side of the assignment, then assigns
it to the left side. So if a=1 and b=2, then
a, b = b, a+b
a,b = 2, 1+2
a,b = 2, 3
a = 2
b = 3
> different to ... a = b
> ... b = a+b
>
Here, you're evaluating sequentially.
a = b
a = 2
b = a + b
b = 2 + 2
b = 4.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list