Help ! newbie problem

F

ferdydurke

hi,

I am trying to learn python... I can't explain the difference beetween these
two programms, Fibonacci suites :

.... print b,
.... a,b,c = b,a+b,c+1
....
1 2 3 5 8 13 21 34 55 89 144

and
.... print b,
.... a=b
.... b=a+b
.... c=c+1
....
1 2 4 8 16 32 64 128 256 512 1024


how about the instructions on the same block ? I thought they could be on
the other lines... perhaps I don't understand a=b and b=a+b ?


thanks in advance.
 
R

Raymond Hettinger

I am trying to learn python... I can't explain the difference beetween these
two programms, Fibonacci suites :


... print b,
... a,b,c = b,a+b,c+1
...
1 2 3 5 8 13 21 34 55 89 144

and

... 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:
.... print b,
.... tmp1 = b
.... tmp2 = a+b
.... tmp3 = c+1
.... a = tmp1
.... b = tmp2
.... c = tmp3


Raymond Hettinger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top