The difference between “==” and “is” in Python
Python’s ‘==’ and ‘is’ are commonly thought of as equality operators. While this may look correct, it is not exactly true. The ‘is’ is actually an identity keyword. The ‘==’ operator is used when determining whether two variables store the same value while the ‘is’ keyword determines whether two variables store the same value in a computer’s memory.
In my previous medium post about python variables, I described a variable as a box that a value is stored inside of. This doesn’t really explain what happens😶. It turns out that python has assigned different values already to specific places in a computer’s memory. In simple terms, if I were to put the number 5 in a variable named ‘alpha’ and I put the number 5 in another variable and named it ‘bravo’, python would simply assign both variable names to the space in memory named ‘5’.
>>> alpha = 5
>>> bravo = 5
Now, if we were to compare the equality of both variables with the ‘==’ operator, we would get True as the output because of course the two values are the same.
>>> alpha == bravo
True
What if we used the ‘is’ keyword? We would still get True as output. Remember I said earlier that the ‘is’ keyword determines whether two variables store the same values in a computer’s memory. Since these two variables are pointing to the same space in memory, the output is True.
>>> alpha is bravo
True
So how do we know that these two objects store the same value? Well, python has an ‘id’ function that tells us where exactly a value is stored in memory. It is like an address. So if we call the id function on these two variables:
>>> id(alpha)
140709619484448
>>> id(bravo)
140709619484448
They return the same id. This actually means that these two variables are storing the same object😂.
So does this mean that python has assigned all the numbers in the world a space in memory? Of course not. Python only keeps numbers from -5 to 256 in memory. This means that if you were to store a number greater than 256 or less than -5 in two different variables, python would have them duplicated in memory.
>>> charlie = 729
>>> delta = 729
>>> id(charlie)
1957526993008
>>> id(delta)
1957526992944
So what does this mean for our operators? Would both operators return True as output when compared? Let’s see:
>>> charlie == delta
True
>>> charlie is delta
False
The ‘is’ keyword returned False because the number stored in these two variables were stored in different places in the computer’s memory. See the difference?😉
This same analogy cannot be applied to other datatypes like lists, tuples, etc. In this case, taking lists as an example, storing the same list in two different variables does not mean they will have the same ‘address’ when you run the ‘id’ function on both variables.
>>> echo = [1, 2]
>>> foxtrot = [1, 2]
>>> id(echo)
1957526901568
>>> id(foxtrot)
1957533751616
You can see that both lists contain the same values but they are stored in different places in the computer’s memory. If we run ‘==’ and ‘is’:
>>> echo == foxtrot
True
>>> echo is foxtrot
False
The output is not surprising right? But what if we created another variable and stored one of the previous named variables inside:
>>> golf = echo
>>> id(echo)
1957526901568
>>> id(golf)
1957526901568
>>> golf == echo
True
>>> golf is echo
True
Both returned True as output. This is because when ‘echo’ was put in the ‘golf’ variable, python simply made the variable ‘golf’ point to the space in memory that ‘echo’ was pointing to initially. Since both variables point to the same space in memory, they would have the same ‘id’.
String datatypes does not seem to have any problems as far as I have experimented. It always returns True for both operators no matter how long the string is🤔
>>> hotel = 'aa' * 2000
>>> india = 'a' * 4000
>>> juliet = 'aa' * 2000
>>> id(hotel)
2527098241152
>>> id(india)
2527098241152
>>> id(juliet)
2527098241152
>>> hotel == india
True
>>> hotel == juliet
True
>>> india == juliet
True
>>> hotel is india
True
>>> hotel is juliet
True
>>> india is juliet
True
Maybe I’m missing something?😐 Comment below!
Conclusion
It really is about the ‘id’ function when determining whether the identity keyword ‘is’ will return True or False as output. The ‘id’ could even mean identity😏. The ‘==’ operator returns True when the values are the same.
Thanks for reading:).