Skip to content

Truth expressions

These binary variables allow the state True (true) or False (false). They can be concatenated using and (and) and or (or) as in normal language usage:

Example
True and False # False
True or False # True

Here, 'and' takes precedence over 'or':

Example
(True or False) and False # False
True or (False and False) # True
True or False and False # True

To improve the readability of the code, it is advisable to always use explicit brackets. Otherwise it is easy to lose track of complex conditions .

Conversion

As in other programming languages, many data types are implicitly converted into truth expressions. The following applies to numbers: zero is false, everything else is true. For strings, only the empty string is False.

Example
bool(2) # True
bool(-1) # True
bool(0) # False
bool(0.) # False
bool("True") # True
bool("False") # True
bool("") # False

Analogue to the rule "if it is nothing or zero, then it is False", containers are False if they are empty:

Example
bool([]) # False
bool([False]) # True
bool({}) # False
bool(set()) # False