O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Truth Value Testing

Any object can be tested for truth value, to use in an if or while condition or as operand of the boolean operations below. The following values are considered false:

  • None

  • Zero of any numeric type, e.g., 0, 0L, 0.0

  • Any empty sequence, e.g., ‘ ’, (), []

  • Any empty mapping, e.g., {}

  • Instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns zero

All other values are considered true, so objects of many types are always true.

Operations and built-in functions that have a boolean result always return for false and 1 for true, unless otherwise stated. Important exceptions are the boolean operations or and and, which always return one of their operands.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required