Help ! newbie problem

Raymond Hettinger python at rcn.com
Fri Dec 26 19:43:59 EST 2003


> I am trying to learn python... I can't explain the difference beetween these
> two programms, Fibonacci suites :
> 
> 
> >>> a,b,c = 1,1,1
> >>> while c<12:
> ...     print b,
> ...     a,b,c = b,a+b,c+1
> ...
> 1 2 3 5 8 13 21 34 55 89 144
> 
> and
> 
> >>> a,b,c = 1,1,1
> >>> while c<12:
> ...     print b,
> ...     a=b
> ...     b=a+b
> ...     c=c+1
> ...
> 1 2 4 8 16 32 64 128 256 512 1024

In the first program, the each element on the right hand side of the
equal sign is evaluated before any assignments are made.  So, the
value of "a" is updated *after* "a+b" is computed.

In the second program, "a" is updated *before* "a+b" is computed.

So, the first program can be expanded to something like this:

>>> a,b,c = 1,1,1
>>> while c<12:
...     print b,
...     tmp1 = b
...     tmp2 = a+b
...     tmp3 = c+1
...     a = tmp1
...     b = tmp2
...     c = tmp3


Raymond Hettinger




More information about the Python-list mailing list