In Python an integer 0 represents false, and an integer 1 represents true.
Python recognizes any empty data structure as false and any nonempty data structure as true.
The notions of true and false are intrinsic properties of every object in Python.
Each object is either true or false, as follows:
The following table gives examples of true and false values of objects in Python.
Object | Value |
---|---|
"test" | True |
"" | False |
[1, 2] | True |
[] | False |
{'a': 1} | True |
{} | False |
1 | True |
0.0 | False |
None | False |
Because objects are true or false themselves, it's common to see Python programmers code tests like
if X:,
which, assuming X is a string, is the same as if X != '':.
You can test the object itself to see if it contains anything, instead of comparing it to an empty.