[Tutor] Problem with Fibonacci example

bob gailer bgailer at gmail.com
Sun Mar 10 19:03:40 CET 2013


On 3/10/2013 3:06 AM, bessenkphilip wrote:
> Hello Bob,
>
Hi. When you have a Python question that is not related to Python 
Pipelines I suggest you send it to the tutor group.I and others will see 
it and you will get faster help.

>  While trying one Fibonacci example in tutorial, i'm having some 
> doubts. The program is as below
> >>> a, b = 0, 1
> >>> while b < 10:
> ...    print b
> ...    a, b = b, a+b
>
> Here i tried the last line as separate lines as:
> a = b
> b = a + b
>
> but it returned an error in the first line itself( a = b)
Always post the traceback. I can't help you here without that. Also show 
the program, or sequence of statements entered at >>>.

> From the c programs, i think we use a variable "c" for assigning such 
> that "a = b" and "b =c" where 'c' is the sum of (a+b).
>
> In the python example, the line .. a, b = b, a+b
> i interpreted this as "a = b" and "b = a + b" but as the c programs, 
> won't 'a' already got b's value first.? if that then the line " b =a 
> +b" will be like 'b= b + b" (as a already got b's value)
Python evaluates what's on the right side of the = first. b, a+b is an  
expression list. The result (the first time) is a tuple (1, 0+1) which 
is (1, 1).
Then it evaluates the assignment (=). SInce the target is 2 variables it 
"unpacks" the tuple. The result is
a = 1
b = 1

-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list