Skip to main content
Practice

Checking if Two Objects Are Identical with the 'is' Operator

In Python, everything is an object.

An object encompasses data and the associated actions (functions or methods) related to that data.

The is operator checks if two objects are the same object in memory.

Computer memory is a space where data is temporarily stored while a program is running. Variables are stored in memory, and Python accesses these values by referencing their memory addresses.


The is operator, verifies if the objects being compared are the same or differ from the == operator.

While the == operator compares whether the values of two objects are equal, the is operator compares if the two objects are exactly the same object.

For example, to check if x and y are the same object, you would use x is y.

Example of Using the 'is' Operator
x = [1, 2, 3]

y = [1, 2, 3]

print("x == y:", x == y) # True, the values of x and y are equal

print("x is y:", x is y) # False, x and y are different objects (different memory addresses)

In the example above, variables x and y have identical values but are distinct variables, thus stored in different memory addresses.

Therefore, x is y returns False.

On the other hand, the == operator checks if the values of the two objects are the same, so x and y, which both have the value [1, 2, 3], return True for x == y.

In this case, even if two items have the same value, == may return True, while is may return False.

It is advisable to use == for simple value comparisons and is to strictly check if two objects are identical.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.