Python Identity Operators
Introduction
Identity operators are used to compare the memory locations of two objects in Python. Unlike comparison operators that compare values, identity operators check whether two variables refer to the same object in memory.
Identity operators are particularly useful when working with objects, lists, dictionaries, classes, and special values such as None.
Python provides two identity operators:
isis not
In this tutorial, you will learn about Python identity operators, their syntax, examples, practical applications, and best practices.
What are Identity Operators?
Identity operators determine whether two variables point to the same object in memory.
Example
x = [1, 2, 3]
y = x
print(x is y)
Output
True
Both variables reference the same object in memory.
Types of Identity Operators
Python provides two identity operators:
| Operator | Description |
|---|---|
| is | Returns True if both variables refer to the same object |
| is not | Returns True if both variables refer to different objects |
The is Operator
The is operator checks whether two variables reference the same object.
Syntax
object1 is object2
If both variables point to the same object, Python returns True.
Example
a = [10, 20, 30]
b = a
print(a is b)
Output
True
Both variables reference the same list object.
Memory Representation
a ──► [10, 20, 30]
▲
│
b ─────┘
Both variables point to the same memory location.
The is not Operator
The is not operator checks whether two variables refer to different objects.
Syntax
object1 is not object2
Example
a = [10, 20, 30]
b = [10, 20, 30]
print(a is not b)
Output
True
Although the values are identical, the objects are different.
Identity Operators vs Comparison Operators
This is one of the most important concepts for beginners.
Comparison Operator (==)
Checks whether values are equal.
Example
a = [10, 20, 30]
b = [10, 20, 30]
print(a == b)
Output
True
The values are equal.
Identity Operator (is)
Checks whether both variables point to the same object.
Example
a = [10, 20, 30]
b = [10, 20, 30]
print(a is b)
Output
False
The objects are different even though the values are the same.
Understanding Object Identity
Every object in Python has:
A value
A memory location (identity)
The id() function returns the memory identity of an object.
Example
a = [1, 2, 3]
b = a
print(id(a))
print(id(b))
Output
140252123456
140252123456
Both IDs are the same.
Example
a = [1, 2, 3]
b = [1, 2, 3]
print(id(a))
print(id(b))
Output
140252123456
140252123789
The IDs are different.
Identity Operators with Integers
Python sometimes stores small integers in memory for optimization.
Example
a = 10
b = 10
print(a is b)
Output
True
Python may reuse the same memory object.
Example
a = 1000
b = 1000
print(a is b)
Output
May be True or False
This behavior can vary depending on Python implementation.
For numeric value comparison, use == instead of is.
Identity Operators with Strings
Example
name1 = "Python"
name2 = "Python"
print(name1 is name2)
Output
True
Python often stores identical strings efficiently.
Example
name1 = "Python Programming"
name2 = "Python Programming"
print(name1 == name2)
Output
True
Identity Operators with Lists
Lists are commonly used to demonstrate identity.
Example
list1 = [1, 2, 3]
list2 = list1
print(list1 is list2)
Output
True
Example
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(list1 is list2)
Output
False
Because they are separate objects.
Identity Operators with Dictionaries
Example
student1 = {"name": "John"}
student2 = student1
print(student1 is student2)
Output
True
Example
student1 = {"name": "John"}
student2 = {"name": "John"}
print(student1 is student2)
Output
False
Different dictionary objects.
Using Identity Operators with None
Checking for None is the most common real-world use of identity operators.
Example
value = None
print(value is None)
Output
True
Example
value = "Python"
print(value is not None)
Output
True
Why Use is None Instead of == None?
Recommended
if value is None:
print("No Value")
Not Recommended
if value == None:
print("No Value")
Python’s style guide recommends using is None.
Identity Operators in Conditional Statements
Example
user = None
if user is None:
print("User Not Found")
Output
User Not Found
Example
session = "active"
if session is not None:
print("Session Running")
Output
Session Running
Real-World Example: Database Record Validation
record = None
if record is None:
print("Record Not Available")
Output
Record Not Available
Real-World Example: API Response Validation
response = None
if response is None:
print("API Response Missing")
Output
API Response Missing
Identity Operators in Automation Testing
Identity operators are often used in Selenium and API automation when checking whether objects exist.
Example
element = None
if element is None:
print("Element Not Found")
Output
Element Not Found
Example
driver = object()
if driver is not None:
print("Browser Started")
Output
Browser Started
Common Mistakes Beginners Make
Using is Instead of ==
Incorrect
a = 1000
b = 1000
print(a is b)
This checks identity, not value.
Correct
print(a == b)
This checks whether the values are equal.
Assuming Equal Values Mean Same Object
Example
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(list1 == list2)
Output:
True
print(list1 is list2)
Output:
False
Equal values do not necessarily mean identical objects.
Best Practices
Use == for Value Comparison
if age == 18:
Use is for Identity Checks
if value is None:
Use is not None
if response is not None:
Avoid Using is with Numbers and Strings
Identity behavior may vary.
Advantages of Identity Operators
Compare object identities directly
Useful for checking
NoneHelpful when working with objects and references
Improve code readability
Commonly used in object-oriented programming
Conclusion
Identity operators are used to determine whether two variables reference the same object in memory. Python provides two identity operators: is and is not.
Unlike comparison operators (== and !=), identity operators focus on object identity rather than object value. They are especially useful when working with objects, references, and checking for None.
Understanding the difference between value comparison and identity comparison is an important step toward mastering Python programming.
Frequently Asked Questions (FAQs)
What are identity operators in Python?
Identity operators check whether two variables refer to the same object in memory.
Which identity operators are available in Python?
is
is not
What does the is operator do?
It returns True if both variables reference the same object.
Example:
a = []
b = a
print(a is b)
Output:
True
What does the is not operator do?
It returns True if the variables refer to different objects.
Example:
a = []
b = []
print(a is not b)
Output:
True
What is the difference between == and is?
| Operator | Purpose |
|---|---|
| == | Compares values |
| is | Compares object identities |
Example:
a = [1, 2]
b = [1, 2]
print(a == b)
Output:
True
print(a is b)
Output:
False
Key Takeaways
Identity operators compare object memory locations.
Python provides two identity operators:
isandis not.isreturns True when two variables reference the same object.is notreturns True when variables reference different objects.Identity operators differ from comparison operators.
Use
==for value comparison.Use
iswhen checking object identity.Use
is Noneandis not Nonewhen working withNone.Identity operators are commonly used in object-oriented programming and automation testing.
