Python For Dummies: '==' vs. 'is'
Python Comparison & Identity Operators
I'm sure that in your coding experiences with Python,
whether it be 3 or 2.7, you've wondered to yourself, "What's the difference between is and ==? And why does it matter?" Well, you are in luck because you'll find the answers to those questions and so much more here, so get ready to expand your knowledge of Python! 🎉
The Short Answer
If you're looking for a quick answer to those questions above, then I'll give them to you. I just hope you'll take the time to look a bit more in-depth. 👍
== and != are comparison operators. They compare values.
is and is not are identity operators. They compare memory location.
Keep reading for the long answer. 👀
One of the first lessons you
should might learn in a traditional Python course is that the = sign is not to be misunderstood; doing so could lead to bad practices in your coding experience. Take this short bite of code for example:
Here the variable declarations are not to be thought of as " x is equal to 5" or "y is equal to 10." The proper convention is: "The value of x is now 5" and "The value of y is now 10." You should only think of equals when comparing two or more values, as shown in the
is_value_equal(a, b) function. In short, = is value, and == is equals. This lesson goes for all of Python's comparison operators; ==, !=, <>, >=, <=, >, <.
This lesson might not be as prominent when learning Python
hence why you clicked on this learn page but the is and is not operators have completely different comparison properties than the comparison properties. Here's another code example for you to look at:
If you saw this and thought, "That's gonna return True!", I'm sorry to say that, no, it wouldn't. You see, the is and is not operators do not compare values, nor do they compare data types, nor do they compare memberships like the in and not in operators do. They compare the memory locations of two data types.
Everything in Python is an object, and each object is stored at a specific memory location. The Python is and is not operators check whether two variables refer to the same object in memory. You can see the memory location of an object with
id(). Take a look at this code sample:
Since the built-in
id() function returned two different memory locations ID numbers, the statement
if (var1 is var2) would return False, always. Except when it doesn't.
Special Cases of Identical Identities
Oddly enough, Python interns (saves) objects with commonly-used values (for example, the integers -5 to 256.) This is done to save memory, but the location can vary based on your implementation of Python, like CPython for example.
The most superior implementation, FYI.
But what about when an object has the value of another object, like a copy? Well, that would lead to both objects having the same identity, or memory locations. More specifically, one object would point to the identity of the other.
A Brief Outro
Now, this lesson is one of comparisons and comparisons of said comparisons (🤔) and I could go on about memory locations and identities and interning in Python all day, but that's not what you came for.
I think. (I might do another learn page on that topic alone.) I hope that this helps you and adds to your arsenal of Python conventions.
Happy coding! 👏👏