Did you know that two values in Python can look the same but still be completely different at their core? Two unique and powerful operators set Python apart from other programming languages: identity and membership operators. In this blog, we will be talking about identity operators. But before jumping into these operators, I recommend reviewing concepts like variables, dynamic typing, and memory management, as they will help you grasp these operators more effectively.
Table of Contents
What is Identity Operator?
As the name suggests, identity operators deal with the identity of objects. They check whether two values share the same identity. Identity means memory comparison.
Let’s try to understand this with a real-world example. In India, we have unique identification numbers like the Aadhaar card. Two people might have the same name, surname, or even date of birth, but their unique IDs will differ. Similarly, in Python, identity operators compare values based on their unique memory locations rather than their appearance or value.
The identity operators are:
is
: Checks if two variables point to the same memory location.is not
: Checks if two variables point to different memory locations.
The is
Operator
The syntax for using identity operators is straightforward. You write a value, followed by the keyword is
, which is predefined in Python.
some_value is some_other_value
PythonFor example, if I compare the values 7
and 77
, I will get False
because they are not the same object in memory.
a = 5
b = int(input()) # user Input 5
# Check if a and b are the same object
print(a is b) # Output: True
PythonNow, let’s say I have a variable a
with value 5
, and I want to create another variable b
by asking the user for input. When I execute the comparison a == b
, I might get True
if the user inputs 5. However, if I check a is b
, I will also get True
because both a
and b
point to the same memory address.
To check the memory address in Python, we can use the id()
function. By passing a
into this function, we can see its memory address. If I check the IDs of both a
and b
, they will be the same, confirming that they reference the same object in memory.
# Check memory addresses
print(id(a)) # Output: 140709289465744 (Memory address of a)
print(id(b)) # Output: 140709289465744 (Memory address of b)
PythonIn other programming languages, memory addresses are often displayed in hexadecimal format, but Python presents them in a more user-friendly decimal format.
The is not
Operator
Next, we have the is not
operator, which is the opposite of is
. If a is b
returns True
, then a is not b
will return False
.
a = 5
b = 5
# Check if a and b are the same object
print(a is not b) # Output: False
PythonExample with Lists
Let’s consider another example. Suppose I create a list x
and another list y
.
# Comparing lists
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y) # True, as the values are identical.
print(x is y) # False
PythonIf both lists contain the same elements, the comparison x == y
will return True
. However, if I check x is y
, it will return False
. This is because when we define x
, it occupies a specific memory address, and y
will occupy a different address even if they contain the same values.
Conclusion
In conclusion, identity operators in Python, namely is
and is not
, are essential tools for understanding how Python manages memory and object identity.
I encourage you to experiment with different data types, such as strings, integers, None, lists, dictionaries, and tuples, to see how these operators behave.
As you experiment with identity operators, consider how Python handles memory allocation and object references. This knowledge will deepen your understanding of Python’s behaviour and improve your programming skills.