do...while loops

Steve Holden sholden at holdenweb.com
Tue Feb 6 08:28:54 EST 2001


"Langa Kentane" <LangaK at discoveryhealth.co.za> wrote in message
news:mailman.981465338.22824.python-list at python.org...
> Why is is it while 1:
> would while 2:
> or while 3: work?
>
> newbie
>
Absolutely. Python has many "true" values, and far fewer "false" ones.

The following is a (non-exclusive) list of values which are false in a
Boolean context:

    0
    ()
    []
    {}
    ""
    None

The idea behind "while 1:" is simply to generate an infinite loop (which is
usually terminated by a break inside the loop body). Therefore "while 2:" or
"while 3:" or "while 'something':" or "while [42]:", and many others, will
work exactly the same way.

However, Python users will recognise "while 1:" instantly, and spend three
weeks discussing on c.l.py why this is or is not the best way to express the
concept.

regards
 Steve





More information about the Python-list mailing list