while var, but var ==16 != true

Tim Roberts timr at probo.com
Mon Jul 14 01:32:29 EDT 2008


maestro <notnorwegian at yahoo.se> wrote:
>
>why does this work?  "while p" = "while p != 0" ? 1 is True and 0 is
>false in python but other numbers have no boolean value so why doesnt
>it abort.

Because your statement is incorrect.  Everything has a boolean value in
Python.  0, None, False, '' (empty string), [] (empty list), () (empty
tuple), and {} (empty dictionary) all have a False value.  Everything else
has a True value.

Python didn't even have a boolean type (True and False) until rather
recently (2.2?). 

This is a very handy feature, and it's one of the things I love about
Python.

>so obv while var means while not empty or why not zero but it isnt
>something youd guess unless youd beeen shown it.

It's clearly stated in the documentation.  I don't know how you concluded
that True and False were the only boolean values.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list