Python
!= operator
is not operator
comparison operators
Python programming

Python operation vs is not

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

!= and is not look similar, but they answer completely different questions in Python. != asks whether two values are unequal, while is not asks whether two references point to different objects.

Use != for Value Comparison

!= is the normal operator for comparing content or value. It relies on equality logic, which may be built in or provided by a class through __eq__.

python
1a = [1, 2, 3]
2b = [1, 2, 3]
3
4print(a != b)

This prints False because the lists contain the same values, even though they are separate objects.

For everyday program logic, != is usually what you want:

python
1status = "done"
2
3if status != "pending":
4    print("continue workflow")

That is a value question, not an identity question.

Use is not for Identity Checks

is not compares object identity. It tells you whether two names refer to two different objects.

python
1a = [1, 2, 3]
2b = [1, 2, 3]
3
4print(a is not b)

This prints True because the two lists were created separately.

The most common real use of is not is with singletons, especially None:

python
1value = "hello"
2
3if value is not None:
4    print("value is present")

That is the idiomatic style because None is a singleton, so identity is the right concept.

Why the Difference Matters

Custom classes make the distinction obvious:

python
1class Point:
2    def __init__(self, x, y):
3        self.x = x
4        self.y = y
5
6    def __eq__(self, other):
7        return isinstance(other, Point) and self.x == other.x and self.y == other.y
8
9
10p1 = Point(1, 2)
11p2 = Point(1, 2)
12
13print(p1 != p2)
14print(p1 is not p2)

The first comparison says the points are equal in value. The second says they are different objects. Both answers can be correct at the same time.

Do Not Trust Identity for Numbers and Strings

Python sometimes interns small integers and some strings. That means identity checks can appear to work for value comparisons by accident:

python
1x = 256
2y = 256
3
4print(x is y)

That result is an implementation detail, not a reliable value-comparison rule. If you care about equality, use == or !=.

The same caution applies to NumPy arrays and pandas objects. != may perform element-wise comparison, while is not only compares whether the two variables reference the same object.

Common Pitfalls

The biggest mistake is using is not for strings, numbers, or other value comparisons. That can appear to work sometimes and fail later when object identity happens to differ.

Another common issue is writing value != None instead of value is not None. For None, identity checks are clearer and more idiomatic.

People also confuse "equal" with "the same object". Two separate instances can be equal in value but still be different objects in memory.

Finally, when reviewing code, ask which question is actually being asked: "same value" or "same object". That almost always tells you which operator belongs there.

That small mental check prevents a surprising number of bugs in Python code reviews, especially around strings, sentinels, and optional values.

Summary

  • Use != when you want to compare values.
  • Use is not when you want to compare object identity.
  • 'is not None is the standard way to check for a non-missing value.'
  • Do not rely on identity checks for numeric or string value logic.
  • Two objects can be equal in value and still be different objects.

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.