False objects

All Python objects evaluate as true except for null, empty, and zero objects. The following objects evaluate as false:

• False (one of the two Boolean objects)

• None (a special Python object that stands for "no value") Testing an object for truth

Testing whether an object is true or false is simple. Just use an if block:

1. Type if followed by a literal or the name of an object. Then type a colon and press Return:

3. Type four spaces, and then type something for Python to do. Press Return twice when you're finished.

Python performs the action only when the object evaluates as true. This test does not assume that the object is any particular data type. Because you're not comparing the object against anything else, you can think of it as "letting the object evaluate itself."

TECHNICAL if name of object: is short for if bool(name of object) STUFF == True.

Was this article helpful?

0 0

Responses

  • Carl
    Do null objects evaluate to false in python?
    3 years ago

Post a comment