Python operation vs is not
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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__.
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:
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.
This prints True because the two lists were created separately.
The most common real use of is not is with singletons, especially None:
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:
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:
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 notwhen you want to compare object identity. - '
is not Noneis 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
- Python os.path.join on a list
- python pandas apply a function with arguments to a series
- Python Pandas Convert .value_counts output to dataframe
- Python pandas Filtering out nan from a data selection of a column of strings
- Python Pandas Get index of rows where column matches certain value
- Python Pandas How to read only first n rows of CSV files in?
- Python Pandas merge only certain columns
- python pandas remove duplicate columns
.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.