Booleans
Booleans are either true or false. Python has two constants, cleverly named True
and False
, which can be used to assign boolean values directly. Expressions can also evaluate to a boolean value. In certain places (like if
statements), Python expects an expression to evaluate to a boolean value. These places are called boolean contexts. You can use virtually any expression in a boolean context, and Python will try to determine its truth value. Different datatypes have different rules about which values are true or false in a boolean context. (This will make more sense once you see some concrete examples later in this chapter.)
You can use virtually any expression in a boolean context.
For example, take this snippet from humansize.py
:
Create a free account to view this lesson.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy