Difference Between '==' And 'is' Operator In Python

Explore the key differences between '==' and 'is' operators in Python. Learn how they compare values and object identities with practical examples.

Python, known for its readability and efficiency, offers a variety of operators to compare values. Among these, '==' and 'is' are often sources of confusion for beginners and even experienced developers. This blog demystifies the difference between these two operators.

What Is '==' Operator?

The '==' operator in Python is a comparison operator used to check if the values on either side of it are equal. It evaluates to True if the values are equal and False if they are not.

Characteristics of '==' Operator

  • Value Comparison: It compares the values of two objects, disregarding their memory addresses.
  • Type Agnostic: Works across different data types, as long as the values are comparable.
  • Operator Overloading: Can be customized for user-defined classes using the eq method.

Examples of '==' operator.

Comparing Numbers:

print(10 == 10)  # Output: True
print(10 == 20)  # Output: False

Comparing Strings:

print("Python" == "Python")  # Output: True
print("Python" == "python")  # Output: False

Comparing Lists:

list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(list1 == list2)  # Output: True
list3 = [1, 2, 4]
print(list1 == list3)  # Output: False

The '==' operator in these examples, checks if the values on the left and right sides are the same, regardless of whether they are the same object in memory.

What Is The 'Is' Operator?

The 'is' operator in Python is used to check if two variables refer to the same object in memory. It compares the identities of the objects, not their values.

Characteristics of 'is' Operator

  • Identity Comparison: It checks whether two variables point to the same memory location.
  • Strict Comparison: Unlike '==', which can be overridden, 'is' always compares object identities.
  • Commonly Used with Singleton Objects: It's frequently used to compare with singletons like None.

Examples of 'is' Operator

Comparing Variables Referring to the Same Object:

a = [1, 2, 3]
b = a
print(a is b)  # Output: True

Comparing Different Objects:

a = [1, 2, 3]
b = [1, 2, 3]
print(a is b)  # Output: False

Checking for None:

x = None
y = None
print(x is y)  # Output: True

In these examples, the 'is' operator is used to determine if two variables point to the same object, not just objects with identical contents. This distinction is crucial for understanding how Python handles variable references and memory allocation.

Identity Vs Equality Operators

In Python, the 'is' operator is the identity operator, while '==' is the equality operator. They serve different purposes and are used in distinct scenarios.

Identity Operator ('is')

  • Purpose: The 'is' operator checks whether two variables refer to the exact same object in memory.
  • Usage: It's ideal for confirming that two references are truly pointing to the same object.
  • Common Use Case: Often used to compare with singleton objects like None.

Example.

x = [1, 2, 3]
y = x
print(x is y)  # Output: True

Here, x and y refer to the same list in memory, so x is y is True.

Equality Operator ('==')

  • Purpose: The '==' operator checks whether the values of two objects are equal, regardless of their identity.
  • Usage: Used when you want to confirm that two objects, possibly different, have the same value.
  • Flexibility: Can be overridden in custom classes to define custom equality logic.

Example.

a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)  # Output: True

In this case, a == b is True because the lists contain the same elements, even though they are different objects in memory.

In summary, 'is' compares identities, and '==' compares values. Understanding this distinction is crucial for writing correct and efficient Python code.

You can also check these blogs:

  1. Membership and Identity Operators In Python