is and is not Identity Operators# Identity check using 'is' and 'is not'
a = 30
b = 30
print(a is b) # True (Small integer caching)
x = 300
y = 300
print(x is y) # False (Different object memory addresses)
print(x == y) # True (Values are equal)
x is y and x is not y The is operator returns True if both operands point to the exact same object instance (matching memory address), whereas == compares equality of values. The is not operator returns True if the operands refer to distinct objects. For sequence memberships, see in and not in, or check types using type().
| Operator | Description | Example |
|---|---|---|
is | Returns True if both variables point to the same object in memory | a is b → True/False |
is not | Returns True if variables point to different objects in memory | a is not b → True/False |
result = x is y
result = x is not y
Parameters: x and y are any Python objects or variables being compared for memory address identity.
A common source of confusion in Python is distinguishing between is (identity check) and == (value check):
# Example 1: Negative numbers outside small integer cache
a = -6
b = -6
print("Using is:", a is b) # False
print("Using ==:", a == b) # True
# Example 2: Integers between -5 and 256 are cached by CPython
a = 30
b = 30
print("a is b (30):", a is b) # True
a = 300
b = 300
print("a is b (300):", a is b) # False
print("a == b (300):", a == b) # True
You can use identity operators to verify data types, though built-in functions like isinstance() are generally preferred for inheritance support:
x = 500
print(type(x) is int) # True
x = 100.34
print(type(x) is int) # False
print(type(x) is float) # True
y = 400
print(type(x) is type(y)) # False (float vs int)
# List and collections type checking
my_list1 = [1, 2, 3]
my_list2 = [4, 5, 6]
print(type(my_list1) is type(my_list2)) # True
CPython pre-allocates and caches small integer objects ranging from -5 to 256 at startup. Any integer within this range shares the exact same memory address across variable assignments. Integers outside this range (like 300) are allocated as separate objects in memory.
Use is strictly when comparing object identities against singletons like None (e.g., if x is None:) or when checking exact object references. For values, strings, and numbers, always use ==.
Yes, is is a simple pointer comparison (comparing memory addresses), whereas == invokes Python's __eq__() dunder method, which evaluates content equality and can be slower for complex objects.
Test object identity comparisons, small integer caching behavior, and type checks in our interactive Google Colab companion notebook:
Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:
View on GitHubAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.