Python Built-In Modules: Truth Value Testing

106 50


Truth value testing is a binary by nature. Either something is or is not. Every object in Python may be tested for its truth value. This test then forms the protasis of conditional loops such as if or while.

The following values are naturally considered false in Python:
  • None
  • False
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  • any empty sequence (e.g., '', (), or []).
  • any empty mapping, for example, {}.


  • instances of user-defined classes, if the class defines either a __nonzero__() or __len__() method, when that method returns the integer zero or a boolean value False.

All other values are considered to be true.

NB: Boolean results from operations or functions almost always return 0 or False for false and 1 or True for true. One major exception to this is the boolean operations or and and always return one of their operands.
Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.