Python
None
Comparison
Equality
Programming Tips

Python None comparison should I use is or ?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

When you want to test whether a value is None in Python, the idiomatic and correct answer is to use is or is not, not ==. The reason is that None is a singleton object, and identity checks express exactly what you mean.

Use is None

The standard pattern is:

python
1value = None
2
3if value is None:
4    print("value is missing")

And the negative form is:

python
if value is not None:
    print("value is present")

This is the form recommended by Python style guides and widely expected by other Python developers.

Why is Is Better Than ==

is checks object identity. It asks whether two references point to the exact same object.

== checks equality. It asks whether two objects should be considered equal according to their value semantics.

For None, you do not want value equality. You want to know whether the object is literally the special singleton None.

python
print(None is None)   # True
print(None == None)   # True

Both happen to be true here, but they mean different things.

The Real Problem with == None

The danger is that == can call custom equality logic. User-defined classes may implement __eq__ in ways that produce surprising results.

python
1class WeirdValue:
2    def __eq__(self, other):
3        return other is None
4
5
6item = WeirdValue()
7
8print(item == None)      # True
9print(item is None)      # False

That example is intentionally strange, but it shows why is None is more reliable. It cannot be faked by overloaded equality behavior.

This Also Helps Readability

if x is None: communicates intent clearly. It tells the reader you are checking for the special sentinel object, not comparing general values.

That is especially important in code where None means something specific, such as:

  • optional function arguments
  • missing database fields
  • "not yet computed" cache entries
  • sentinel return values

Clear intent matters more than tiny syntax differences.

Do Not Use Truthiness Instead

Another common mistake is writing:

python
if not value:
    ...

That does not mean the same thing. It treats 0, False, [], "", and None the same way.

If you specifically mean None, write that explicitly:

python
if value is None:
    ...

This avoids bugs where valid but empty values get mistaken for missing values.

Typical Function Example

Here is a normal use case with an optional argument:

python
1def greet(name=None):
2    if name is None:
3        name = "guest"
4    return f"Hello, {name}!"
5
6
7print(greet())
8print(greet("Ana"))

This makes the sentinel meaning of None explicit and easy to follow.

What About Singletons Like True and False

The guidance for None is especially strong because it is a singleton sentinel used for absence of value. For booleans, you often want truthiness or explicit boolean logic instead of identity checks.

So this article's rule is not "always use is instead of ==." It is specifically "use is when checking for None."

Common Pitfalls

The biggest mistake is using == None, which can invoke custom equality behavior and obscure your real intent.

Another mistake is using if not value when the code needs to distinguish between None and other falsy values such as 0 or an empty string.

Developers also sometimes compare return values to None indirectly through chained conditions, making the code harder to read than a direct is None check.

Finally, avoid is for ordinary value comparisons like numbers or strings. is is for identity, not general equality.

Summary

  • Use is None and is not None for None checks in Python.
  • 'is checks identity, which matches the meaning of None.'
  • '== can be overridden by custom classes and is less reliable here.'
  • Do not replace a None check with a generic truthiness check unless that is truly what you mean.
  • The rule is specific to sentinel-style identity checks, not to all comparisons in Python.

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.