Learn to Code via Tutorials on Repl.it!

← Back to all posts
Python For Dummies: '==' vs. 'is'
frissyn

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. 👀

Comparison Operators


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; ==, !=, <>, >=, <=, >, <.

Identity 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! 👏👏

IreTheKID

Voters
Kathool
DonaldWise
Loshad
IsmailLimbada
21natzil
Highwayman
frissyn
Comments
hotnewtop
21natzil

Then how come if I do

Food for thought :D. Great tutorial! Well done!

frissyn

@21natzil Because Python, (CPython, at least) stores the values of the integers -5 through 256 in the same memory location. I put that in the "Special cases of Identical Identities" section.

21natzil

@IreTheKID Oh did I miss that? Great job then!

Highwayman

Wait wait wait, so I can technically use pointers in python?

Highwayman

@Highwayman

might do another learn page on that topic alone.)

I would definitely read that! :P

frissyn

@Highwayman Heh, thanks! You can never learn enough in Python! ;)

Highwayman

@IreTheKID definitely! :)