is keyword tests if the left and right operands refer to the same object—in which case it returns
True. It returns
False if they are not the same object, even if the two objects are equal. For example, the expression
[1, 2, 3] is [1, 2, 3] returns
False because although both lists are equal, they are two independent objects in memory.
>>> l1 = [1, 2, 3] >>> l2 = [1, 2, 3] >>> l1 is l2 False >>> l1 == l2 True
The difference between the ‘
is‘ and ‘
==‘ operators is that ‘
is‘ compares the identity of two objects whereas ‘
==‘ compares the equality of two objects.
? Equality follows from identity, i.e., if
x is y, then
Python is Operator Strings
is operator on two strings checks if both refer to the same object in memory. As strings are immutable — that is they cannot be modified after creation — the same string is always represented by the same object in memory. Thus, identity is the same as equality for strings, i.e.,
s1 == s2 is the same as
s1 is s2. This is more efficient as holding two equal string objects in memory. If the strings cannot be changed, all variables could simply refer to the same immutable string object.
The following example shows how to independently “created” string variables really refer to the same object in memory:
>>> s1 = 'hi' >>> s2 = 'hi' >>> s1 is s2 True
Consequently, equal strings are identical strings and identical strings are equal strings:
>>> s1 == s2 True
Python is Operator Integers
Warning: Do not use the identity operator
x is y to compare integers
y in Python. Instead, use the equality operator
x == y. The reason has to do with the internal representation of the identity operator:
# Expected Behavior >>> 3 is 3 True >>> 999 is 999 True >>> 999 is 998 + 1 True
Here’s the problem though: both variables in the following example do not point to the same integer, even though they are equal!
# Unexpected Behavior >>> x = 257 >>> y = 257 >>> x is y False >>> x == y True
However, the explanation is simple. The is operator only checks if both variables refer to the same object.
In the previous code example, you create two variables
y and you shouldn’t actually expect those variables to refer to the same object in memory. This is not guaranteed by Python—it’s only an implementation detail (almost by accident) that those two variables may point to the same object if they are immutable.
However, for efficiency reasons, Python’s creators chose to represent integers from -5 to 256 with the same object reference but not the rest of them. Technically, those “smaller” numbers -5 to 256 use the same array data structure, so all variables pointing to those integers actually point to the same array data structure.
Python Small Integer Caching
Let’s recap the problem by studying a minimal example again:
a, b = 250, 250 for i in range(250, 260): if a is not b: break a += 1 b += 1 print(a) # What's the output of this code snippet?
You’d guess that the for loop goes from
i=259, each time incrementing
b. As Python creates one integer object to which both names refer, the command
a is not b should always be
False. Thus, the result is
The result is
If you create an integer object that falls into the range of -5 to 256, Python will only return a reference to this object — which is already cached in memory.
“The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.”Python Docs
Again, if you create an integer object that falls into the range of -5 to 256, Python will only return a reference to this object — which is already cached in memory. But if we create an integer object that does not fall into this range, Python may return a new integer object with the same value.
If we now check
a is not b, Python will give us the correct result
In fact, this leads to the strange behavior of the C implementation of Python 3:
>>> a = 256 >>> b = 256 >>> a is b True >>> a = 257 >>> b = 257 >>> a is b False
Therefore, you should always compare integers by using the
== operator in Python. This ensures that Python performs a semantic comparison, and not a mere memory address comparison:
>>> a = 256 >>> b = 256 >>> a == b True >>> a = 257 >>> b = 257 >>> a == b True
What can you learn from this? Implementation details do matter!
Python is Operator Overloading
x is y essentially checks if both objects
y refer to the same address in memory. A semantically equivalent way would be to compare the integer representations of the object memory addresses using the
id() function, i.e.,
id(x) == id(y). As the memory address is constant throughout an object’s lifetime, you cannot override it. Consequently, you cannot overload the
The following code indicates that both highlighted ways are semantically identical:
>>> l1 = [1, 2, 3] >>> l2 = [1, 2, 3] >>> id(l1) 2860496429120 >>> id(l2) 2860496429376 >>> id(l1) == id(l2) False >>> l1 is l2 False
Python has two identity operators:
is not. They are used to check if two values or variables reside at the same memory location, i.e., refer to the same object in memory. However, if two variables are equal, this doesn’t imply that they are identical. For example, two lists
[1, 2, 3] and
[1, 2, 3] may be different objects (not identical) but they’re equal in value.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.