What does \'is\' operator do in Python?

The "is" operator in Python is an identity operator that checks whether two variables refer to the same object in memory. It returns True if both variables point to the same object, and False otherwise.

Each object in Python's memory has a unique identification number assigned by the interpreter. The is operator compares these memory addresses using the id() function internally, making it different from the == operator which compares values.

Syntax

variable1 is variable2

The is operator returns True if both variables reference the same object in memory, False otherwise.

Example with Lists

Lists are mutable objects, so identical lists with same values are stored as separate objects ?

numbers1 = [1, 2, 3]
numbers2 = numbers1  # Assignment creates reference to same object
numbers3 = [1, 2, 3]  # Creates new object with same values

print("numbers1 is numbers2:", numbers1 is numbers2)
print("numbers1 is numbers3:", numbers1 is numbers3)
print("numbers1 == numbers3:", numbers1 == numbers3)
numbers1 is numbers2: True
numbers1 is numbers3: False
numbers1 == numbers3: True

Example with Strings

Strings are immutable, so Python often reuses the same object for identical string values ?

text1 = "Python"
text2 = text1
text3 = "Python"

print("text1 is text2:", text1 is text2)
print("text1 is text3:", text1 is text3)
print("id(text1):", id(text1))
print("id(text3):", id(text3))
text1 is text2: True
text1 is text3: True
id(text1): 140234567890480
id(text3): 140234567890480

Comparison with == Operator

Operator Compares Example
is Memory identity [1,2] is [1,2] ? False
== Values/content [1,2] == [1,2] ? True

Common Use Cases

The is operator is commonly used to check for None values ?

value = None

if value is None:
    print("Value is None")
else:
    print("Value is not None")
    
# Better than using == with None
if value == None:  # Not recommended
    print("This works but 'is' is preferred")
Value is None
This works but 'is' is preferred

Conclusion

Use is to check object identity in memory, especially when comparing with None. Use == to compare values. Understanding this difference prevents common bugs in Python programs.

---
Updated on: 2026-03-24T20:12:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements