Understanding Truthiness in Python: How True and False Are Defined Across Versions
The article explains Python's definition of True and False, compares differences between Python 2 and Python 3, demonstrates how various objects evaluate in Boolean contexts, and outlines the language's truth‑value testing rules and custom object behavior.
Python’s definition of the Boolean values True and False varies between versions: in Python 2, None , 0 , and empty strings are considered False ; in Python 3, the same plus empty lists and dictionaries are False , with all other values evaluating to True .
In practice, the logical evaluation may differ from simple equality checks, as shown by the following interactive tests.
<code>>>>bool(0)
False
>>>bool("")
False
>>>bool([])
False
>>>bool({})
False
>>>bool(None)
False</code>When using the equality operator ( == ), the results are not always the same:
<code>>>>0 == False
True
>>>"" == False
False
>>>[] == False
False
>>>{} == False
False
>>>None == False
False
>>>0 == True
False
>>>"" == True
False
>>>[] == True
False
>>>{} == True
False
>>>None == True
False</code>The discrepancy arises because Python follows a set of truth‑value testing rules rather than a simple equality to False . The official documentation states that the following are interpreted as false in Boolean contexts: False , None , numeric zero of any type, and empty strings or containers (lists, tuples, dictionaries, sets, etc.). All other values are true, and user‑defined objects can customize their truth value via a __bool__() method.
Boolean type: False is false, everything else is true.
Integers and floats: 0 is false, other numbers are true.
Strings and string‑like types (including bytes and unicode ): empty string is false.
Sequence types (tuples, lists, dicts, sets, etc.): empty is false, non‑empty is true.
None is always false.
For custom types, Python applies the following rules:
If the class defines __nonzero__() (Python 2) or __bool__() (Python 3), that method is called to determine truthiness.
If no such method exists but __len__() is defined, the object is false when len() returns 0 , otherwise true.
If neither method is defined, the object is considered true, except for None .
These rules are reflected in PEP 8’s recommendation to rely on the falsiness of empty sequences rather than explicit length checks:
<code>For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
Yes: if not seq:
# do something
No: if len(seq):
# do something
</code>*Disclaimer: This article is compiled from online sources; copyright belongs to the original author. If any information is incorrect or infringes rights, please contact us for removal or authorization.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.