Python IS Operator
The is operator checks whether two variables point to the same memory location. If they do, it returns True; otherwise, it returns False. This operator is often used when you need to verify whether two objects are identical.
Syntax
x is y
Here:
- x and y are the objects being compared.
- The expression evaluates to True if x and y reference the same object; otherwise, it evaluates to False.
Example 1: Comparing Two Variables
Using the 'is' operator
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y)
print(x is z)

You can also try this code with Online Python Compiler
Run Code
Output:
True
False
In the above example:
- x and y refer to the same list in memory, so x is y evaluates to True.
- z is a different object with the same content as x, so x is z evaluates to False.
Example 2: Using with Immutable Data Types
Comparing immutable objects
num1 = 10
num2 = 10
num3 = 500
a = 500
print(num1 is num2)
print(num3 is a)

You can also try this code with Online Python Compiler
Run Code
Output:
True
False
For immutable types like integers and strings, Python often optimizes memory usage by caching small values. Therefore, num1 and num2 point to the same memory location for numbers in the range -5 to 256.
Python IS NOT Operator
The is not operator checks whether two variables do not point to the same memory location. If they do not refer to the same object, it returns True; otherwise, it returns False.
Syntax
x is not y
Here:
- x and y are the objects being compared.
- The expression evaluates to True if x and y reference different objects; otherwise, it evaluates to False.
Example 1: Checking Non-Identical Objects
Using the 'is not' operator
x = [4, 5, 6]
y = [4, 5, 6]
z = x
print(x is not y)
print(x is not z)

You can also try this code with Online Python Compiler
Run Code
Output:
True
False
In this example:
- x and y have the same content but are different objects in memory, so x is not y evaluates to True.
- z references the same object as x, so x is not z evaluates to False.
Example 2: Using with Mutable Objects
Checking objects with mutable data types
list1 = [7, 8, 9]
list2 = list1.copy()
print(list1 is not list2)

You can also try this code with Online Python Compiler
Run Code
Output:
True
The copy() method creates a new list object with the same content, so list1 and list2 do not refer to the same object.
Key Differences Between is and ==
It is important to understand the distinction between the is operator and the == operator:
- The is operator checks if two variables point to the same object in memory.
- The == operator checks if two variables have the same value.
Example:
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y)
print(x is y)

You can also try this code with Online Python Compiler
Run Code
Output:
True
False
Common Use Cases of Identity Operators
Identity operators in Python are useful in scenarios, such as comparing singleton objects and working with objects in classes.
1. Comparing Singleton Objects
Identity operators are frequently used to check if a variable points to a singleton object. The most common example is checking if a variable is None. Since None is a singleton in Python (there is only one instance of None), the is operator is ideal for this check.
Example:
value = None
if value is None:
print("Value is None")

You can also try this code with Online Python Compiler
Run Code
Output:
Value is None
In this example, the identity operator is is used to confirm that the value variable points to the singleton None object.
2. Object Comparisons in Classes
When working with custom classes, identity operators help to check if two objects refer to the exact same instance in memory. This is useful in cases where you need to determine if two variables are referencing the same object, not just objects that are equal in value.
Example:
class Example:
pass
obj1 = Example()
obj2 = Example()
obj3 = obj1
print(obj1 is obj2)
print(obj1 is obj3)

You can also try this code with Online Python Compiler
Run Code
Output:
False
True
In this example:
- obj1 and obj2 are different objects created from the same class, so obj1 is obj2 evaluates to False.
- obj3 is assigned the reference of obj1, making them point to the same object in memory, so obj1 is obj3 evaluates to True.
Frequently Asked Questions
What is the difference between is and == in Python?
The is operator checks if two objects point to the same memory location and == checks if two objects have the same value.
When should I use the is operator?
Use the is operator when you need to verify object identity, such as checking if a variable is None or comparing singleton objects.
Can is and is not operators be used with all data types?
Yes, is and is not operators can be used with all data types, but their behavior depends on whether the objects are mutable or immutable.
Conclusion
In this article, we discussed the identity operators in Python: is and is not. These operators help determine whether two variables reference the same object in memory. We discussed their syntax, examples, and use cases. By understanding how identity operators work, we can write more precise and efficient Python code, especially when dealing with objects and memory management.