Python For Dummies: '==' vs. 'is'
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! 🎉
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:
x = 5 y = 10 def is_value_equal(a, b): if a == b: return True else: return False print(is_value_equal(x, y))
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:
x = 500 y = 500 def is_identity_equal(a, b): if a is b: return True else: return False print(is_identity_equal(x, y))
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:
var1 = 1234 var2 = 5678 print(id(var1)) #prints 139785779052784 print(id(var2)) #prints 139785779052816
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.
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.
var1 = 0 var2 = 0 print(id(var1)) #prints 140531377664224 print(id(var2)) #prints 140531377664224
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.
g = [1, 3, 5, 7] h = g print(id(g)) #prints 140265659008512 print(id(h)) #prints 140265659008512
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! 👏👏