Difference between == and is operator in python.

In Python, the == and is operators are used for comparison, but they serve different purposes. The == operator checks for equality of values, which means it evaluates whether the values of two objects are the same. On the other hand, the is operator checks for identity, meaning it determines whether two variables point to the same object in memory.

What Is the 'is' Operator?

The Python is operator tests whether two variables refer to the same object in memory. If the variables refer to the same memory location, it returns True; otherwise, it returns False. This operator is commonly used to check object identity rather than object equality.

Example

The following example demonstrates the is operator in Python ?

# Variables pointing to the same object
var1 = True
var2 = True

# Using 'is' operator
result = var1 is var2
print("var1 is var2:", result)

# Check memory addresses
print("id(var1):", id(var1))
print("id(var2):", id(var2))
var1 is var2: True
id(var1): 140712244357024
id(var2): 140712244357024

What Is the '==' Operator?

The equality (==) operator compares two objects based on their values. If both objects have the same values, the equality operator returns True; otherwise, it returns False. The equality operator focuses on the contents or values of the objects being compared.

Example

Following example shows the == operator in Python ?

# Initialized tuples with same values
tup1 = ("Python", "Java", "CSS")
tup2 = ("Python", "Java", "CSS")

# Equality operator
result = tup1 == tup2
print("tup1 == tup2:", result)

# Check if they are the same object
print("tup1 is tup2:", tup1 is tup2)
tup1 == tup2: True
tup1 is tup2: False

Key Differences Between 'is' and '==' Operators

The is operator and the equality (==) operator serve different purposes in comparison operations ?

# Lists with identical content
list1 = ["Python", "Java", "CSS"]
list2 = ["Python", "Java", "CSS"]
list3 = list1  # Same object reference

print("=== Content vs Identity Comparison ===")
print("list1 == list2:", list1 == list2)  # Same content
print("list1 is list2:", list1 is list2)  # Different objects

print("\nlist1 == list3:", list1 == list3)  # Same content
print("list1 is list3:", list1 is list3)  # Same object

# Memory addresses
print("\n=== Memory Addresses ===")
print("id(list1):", id(list1))
print("id(list2):", id(list2))
print("id(list3):", id(list3))
=== Content vs Identity Comparison ===
list1 == list2: True
list1 is list2: False

list1 == list3: True
list1 is list3: True

=== Memory Addresses ===
id(list1): 140325987654400
id(list2): 140325987654528
id(list3): 140325987654400

Comparison Table

Aspect == Operator is Operator
Purpose Compares values/content Compares object identity
Returns True when Objects have same values Variables reference same object
Memory location Can be different Must be the same
Use case Value comparison Identity checking (None, True, False)

Common Use Cases

The is operator is commonly used with singleton objects like None, True, and False ?

# Checking for None (recommended approach)
value = None
print("value is None:", value is None)
print("value == None:", value == None)

# Both work, but 'is' is preferred for None
print("\n=== Singleton objects ===")
a = True
b = True
print("a is True:", a is True)
print("a == True:", a == True)
value is None: True
value == None: True

=== Singleton objects ===
a is True: True
a == True: True

Conclusion

Use == when you want to compare values or content of objects. Use is when you need to check if two variables reference the exact same object in memory, especially with singleton objects like None.

Updated on: 2026-03-24T20:57:51+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements