Can python find fibonacci series in a single line of code?

Terry Reedy tjreedy at udel.edu
Tue Nov 12 10:42:49 EST 2002


>>  python -c "a=b=1;exec'while+1:print+a;a,b=b,a+b'"

"Bengt Richter" <bokr at oz.net> wrote in message
news:aqqt9t$vu2$0 at 216.39.172.122...
> I didn't know exec would parse semicolons that way (I hardly ever
use it). But
> is that new with 2.2? Does that mean it effectively assumes a
semicolon is the
> same as a newline and indent to the same level as the previous
statement, until
> it finds an actual newline?

No, ; binds tighter than :.
The while statement within ' ' is effectively parsed as

while+1: (print+a;a,b=b,a+b) # except that parens here are not legal
syntax
==
while+1:
    print+a;a,b=b,a+b

Terry J. Reedy






More information about the Python-list mailing list