Boolean

Python, from version 2.3 forward, has a  type which is a subclass of , the standard integer type. It has two possible values:  and , which are special versions of 1 and 0 respectively and behave as such in arithmetic contexts.

Also, a numeric value of zero (integer or fractional), the null value, the empty string, and empty containers (i.e. lists, sets, etc.) are considered Boolean false; all other values are considered Boolean true by default.

Classes can define how their instances are treated in a Boolean context through the special method  (Python 2) or   (Python 3). For containers,  (the special method for determining the length of containers) is used if the explicit Boolean conversion method is not defined.

This article uses content from Boolean