Explain difference between == and is operator in Python.

In Python, == and is are both comparison operators but they check different things. == checks if two objects have the same value (equality), while is checks if two variables point to the same object in memory (identity).

== Operator (Equality)

The == operator compares the values of two objects. If the values are equal, it returns True, regardless of whether they are stored at different memory locations.

is Operator (Identity)

The is operator checks whether two variables refer to the exact same object in memory (same id()). Even if two objects have equal values, is returns False if they are stored at different memory addresses.

Key Differences

Feature == (Equality) is (Identity)
Checks Value equality Object identity (same memory address)
Uses Calls __eq__() method Compares id() of objects
[1] __ [1] True (same value) False (different objects)

Example

The following program demonstrates the difference between == and is ?

list1 = [1]
list2 = [1]
list3 = list1   # list3 points to the SAME object as list1

print("id(list1):", id(list1))
print("id(list2):", id(list2))
print("id(list3):", id(list3))

# == checks VALUE equality
print("\nlist1 == list2:", list1 == list2)  # True (same value [1])

# is checks IDENTITY (same object in memory)
print("list1 is list2:", list1 is list2)   # False (different objects)
print("list1 is list3:", list1 is list3)   # True (same object)

The output of the above code is ?

id(list1): 140380664377096
id(list2): 140380664376904
id(list3): 140380664377096

list1 == list2: True
list1 is list2: False
list1 is list3: True

Notice that list1 and list2 have the same value ([1]) so == returns True, but they are different objects in memory (different ids) so is returns False. Since list3 = list1 makes both point to the same object, both == and is return True.

Conclusion

Use == when you want to compare values (most common case). Use is when you want to check if two variables refer to the exact same object in memory, such as checking for None with if x is None.

Updated on: 2026-03-14T13:02:59+05:30

357 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements