You're probably already familiar with equality: that's the
identity uses the
Let's say we have two variables,
y pointing to two lists:
>>> x = [1, 2, 3] >>> y = [1, 2, 3]
If we say
x == y we're asking about equality:
>>> x == y True
Equality is about whether these two objects represent the same value, whether they're essentially the same thing.
== operator delegates to one of these objects and asks it, "do you represent the same value as the other object?"
That's up to the actual objects to answer.
is operator asks about identity.
>>> x is y False
is operator doesn't even look at the objects that
y point to.
is asks if the variables
y are pointing to the same object.
is operator answers the question do two object references actually point to the same object?
x is y checks the memory location that
y are pointing to (by their
id) and checks to see if those locations are the same.
>>> id(x) 139957046343296 >>> id(y) 139957046343488
y have the same
id in memory, that means we're referencing the same object in two places:
y are actually referring to the same exact object.
In Python, assignment points a variable to an object.
If we assign
>>> x = y
We're now pointing the variable
x to the same object that
y is currently pointing to.
>>> id(x) 139957046343488 >>> id(y) 139957046343488
If we call the
append method on the list that
x points to
We've mutated that list object, but we've also mutated the object that
y points to because both
y point to exactly the same object.
>>> x [1, 2, 3, 4] >>> y [1, 2, 3, 4]
These two objects are equal:
>>> x == y True
But they're also identical (which means they point to the same object):
>>> x is y True
Just as we have equality (
==) and inequality (
>>> x == y True >>> x != y False
we also have is for identity (
is) and unidentity (
is not)... or is it inidentity.
How about non-identity.
>>> x is y True >>> x is not y False
is not operator is one of the few operators in Python that actually has a space inside it.
You really don't see identity used very often.
This is really the most important takeaway about identity and equality: you'll use
== all the time, but you'll almost never use
When comparing two objects, you'll almost always want to check for equality instead of identity.
The place you'll most commonly see
is usedis with
>>> x is None False >>> x is not None True
, there are other places you might see it used, the one place you'll most commonly see it used
There's only one
None value in memory in Python.
We're asking the question "is
x pointing to the one and only
You'll only see
is used with special values where there's only one of them in memory.
I call these sentinel values.
Sentinel objects are considered completely unique.
There's only one of them floating in the memory.
Sentinel values are pretty much the one place you'll see identity used and
None is by far the most common sentinel value in Python.
PEP 8, the Python style guide, says you should use identity to compare with
So you should never
x == None, but instead type
x is None.
That's the convention that we use with
None in Python.
When you want to ask the question "does one object represent the same data as another object", you pretty much always want to use equality (with the
You'll almost never need to ask the question "is one pointer referencing literally the same object as another pointer".
If you do want to ask that question though, you'll check identity (with the
is not operators).
The one time that you really should rely on identity is when comparing to
x is None).
Hello friendly web visitor! 👋
This page is part of Python Morsels, an online Python skill-building service.
The best way to learn is by doing. In the case of Python that means writing Python code. If you'd like to improve your Python skills every week, try out Python Morsels by entering your email below to create an account.
Python Morsels topics pages are free and the first month's worth of exercises is free as well. You don't need to enter payment details to sign up.
You can find explanations of many other Python topics by signing up below.