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

Carl Banks imbosol at vt.edu
Mon Nov 11 15:17:29 EST 2002


David Brown wrote:
r> 
> "Pittaya" <a_human_work at hotmail.com> wrote in message
> news:fa61a3d8.0211110720.16a645f8 at posting.google.com...
>> My Perl-addicted friend shows me that he can find fibanicci series in
>> a single line of code.
>>
>> perl -le '$b=1; print $a+=$b while print $b+=$a'
>>
>> can python do something like this?
> 
> No, I don't think you can make a loop that continuously prints out anything
> within a single line (and without defining another function).

Incorrect.


> But can perl do something like this:
> 
> a, b = 1, 1
> while 1 :
>    a, b = b+a, a
>    print a
> 
> It does the same thing - the big difference is in readability.  Why you
> might want to do this on a single line is beyond me.


Because it's fun!!!


while 1: globals().setdefault("a",0); globals().setdefault("b",1); \
    __import__("sys").stdout.write("%d "% a); a,b=b+a,a


-- 
CARL BANKS



More information about the Python-list mailing list