Python 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
Identity operators are used to compare the memory locations (identities) of two objects, verifying whether they point to the exact same object in Python's memory heap.

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().

OperatorDescriptionExample
isReturns True if both variables point to the same object in memorya is b → True/False
is notReturns True if variables point to different objects in memorya is not b → True/False

1. Syntax & Formal Parameters 🔝

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.

2. Difference Between Object Identity and Value Equality 🔝

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

3. Type Checking with `is` and `type()` 🔝

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

Frequently Asked Questions (FAQ) 🔝

Why does `30 is 30` return True while `300 is 300` can return False?

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.

When should I use `is` instead of `==`?

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 ==.

Is `is` faster than `==`?

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.

Further Reading & Related Tutorials 🔝

Practice in Google Colab 🔝

Test object identity comparisons, small integer caching behavior, and type checks in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer