Python if not vs if
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python, you will frequently see conditions written as if not x and wonder how it differs from if x == False or other comparison-based forms. The distinction matters because Python has a concept called "truthiness" where many different values can evaluate as true or false in a boolean context. Understanding this concept is essential for writing idiomatic Python and avoiding subtle bugs that arise from confusing identity, equality, and truthiness.
Truthiness and Falsy Values
Every Python object has a boolean value. When you use an object in a condition like if x, Python evaluates its truthiness. The following values are considered falsy (they evaluate to False in a boolean context):
Everything else is truthy. This means a non-empty list, a non-zero number, or any object instance evaluates to True in a condition.
The not Operator
The not operator inverts the truthiness of a value. not x returns True if x is falsy, and False if x is truthy:
This is different from if name == False, which checks for strict equality with the boolean False:
if not x vs if x == False
The key difference is that if not x checks truthiness, while if x == False checks equality with the False object. These are not the same:
Output:
Only 0, 0.0, and False itself are equal to False. All other falsy values are falsy but not equal to False.
is not vs !=
Python also has the is and is not operators, which check identity (whether two variables point to the same object in memory) rather than equality:
PEP 8 (Python's style guide) explicitly states that comparisons to singletons like None should use is or is not, never == or !=. This is because custom objects can override __eq__ and produce unexpected results:
When to Use Each Form
Use if not x when you want to check for any falsy value (emptiness, zero, None, False):
Use if x is None when you specifically need to distinguish None from other falsy values:
Use if x == False only when you truly need to check for the boolean False specifically (this is rare):
Common Pitfalls
- Writing
if x == Noneinstead ofif x is None, which can give wrong results with custom__eq__methods - Using
if not xwhen you specifically meanif x is None, accidentally treating0,"", and[]as equivalent toNone - Writing
if x == Trueorif x == Falseinstead ofif xorif not x, which is both non-idiomatic and subtly different in behavior - Forgetting that
0and0.0are falsy, causingif not countto trigger when count is zero rather than missing - Confusing
is not(identity check) with!=(equality check)
Summary
if not xchecks truthiness and returnsTruefor any falsy value (None, 0, "", [],{}, False)if x == Falsechecks strict equality with the booleanFalseand isTrueonly for 0, 0.0, and False- Use
if not xfor general emptiness and falsy checks (the Pythonic approach) - Use
if x is Noneorif x is not Nonewhen you need to specifically check for None - PEP 8 recommends truthiness checks over explicit comparisons in most cases
- Use
isandis notfor singleton comparisons (None, True, False)
Related reading
- Python `if x is not None` or `if not x is None`?
- Python if x is not None or if not x is None?
- Python Image Library fails with message decoder JPEG not available - PIL
- python image recognition
- Python implementation of a graph-similarity-grading algorithm
- Python implementation of Multiple-Choice Knapsack
- Python Implementation of OPTICS Clustering Algorithm
- Python implementation of the Wilson `Score` Interval?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.