Python
Conditional Statements
Programming
Code Comparison
Best Practices

Python if not vs if

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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):

python
1False       # The boolean False
2None        # The null object
30           # Integer zero
40.0         # Float zero
5""           # Empty string
6[]          # Empty list
7{}          # Empty dictionary
8set()       # Empty set
9()          # Empty tuple

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:

python
1name = ""
2
3if not name:
4    print("Name is empty or not provided")
5# This prints because "" is falsy
6
7items = [1, 2, 3]
8
9if not items:
10    print("List is empty")
11# This does NOT print because a non-empty list is truthy

This is different from if name == False, which checks for strict equality with the boolean False:

python
1name = ""
2
3if name == False:
4    print("This will NOT print")
5# "" == False evaluates to False because "" is not equal to False
6
7if not name:
8    print("This WILL print")
9# not "" evaluates to True because "" is falsy

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:

python
1# Values where "not x" is True but "x == False" is False:
2values = [None, "", [], {}, set(), ()]
3
4for val in values:
5    print(f"not {val!r:10s} -> {not val}")
6    print(f"{val!r:10s} == False -> {val == False}")
7    print()

Output:

 
1not None       -> True
2None       == False -> False
3
4not ''         -> True
5''         == False -> False
6
7not []         -> True
8[]         == False -> False

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:

python
1a = None
2
3# Correct: check identity with None
4if a is not None:
5    print("a has a value")
6
7# Incorrect: checks equality, which works but is not idiomatic
8if a != None:
9    print("a has a value")

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:

python
1class Tricky:
2    def __eq__(self, other):
3        return True  # Claims to be equal to everything
4
5t = Tricky()
6print(t == None)   # True (misleading!)
7print(t is None)   # False (correct)

When to Use Each Form

Use if not x when you want to check for any falsy value (emptiness, zero, None, False):

python
1def greet(name):
2    if not name:
3        name = "World"
4    print(f"Hello, {name}!")
5
6greet("")      # Hello, World!
7greet(None)    # Hello, World!
8greet("Alice") # Hello, Alice!

Use if x is None when you specifically need to distinguish None from other falsy values:

python
1def process(value):
2    if value is None:
3        raise ValueError("Value must be provided")
4    if not value:
5        print("Value is empty or zero, but was explicitly provided")
6
7process(0)     # "Value is empty or zero, but was explicitly provided"
8process(None)  # Raises ValueError

Use if x == False only when you truly need to check for the boolean False specifically (this is rare):

python
1# Rare case: a function returns True, False, or None for three states
2result = some_check()
3
4if result is True:
5    print("Passed")
6elif result is False:
7    print("Failed")
8elif result is None:
9    print("Not yet checked")

Common Pitfalls

  • Writing if x == None instead of if x is None, which can give wrong results with custom __eq__ methods
  • Using if not x when you specifically mean if x is None, accidentally treating 0, "", and [] as equivalent to None
  • Writing if x == True or if x == False instead of if x or if not x, which is both non-idiomatic and subtly different in behavior
  • Forgetting that 0 and 0.0 are falsy, causing if not count to trigger when count is zero rather than missing
  • Confusing is not (identity check) with != (equality check)

Summary

  • if not x checks truthiness and returns True for any falsy value (None, 0, "", [], {}, False)
  • if x == False checks strict equality with the boolean False and is True only for 0, 0.0, and False
  • Use if not x for general emptiness and falsy checks (the Pythonic approach)
  • Use if x is None or if x is not None when you need to specifically check for None
  • PEP 8 recommends truthiness checks over explicit comparisons in most cases
  • Use is and is not for singleton comparisons (None, True, False)

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.