python-operadores-identidad

Identity Operators in Python

  • 2 min

Identity operators in Python allow us to verify if two variables refer to the same object in memory. These operators are useful for comparing object identity (i.e., whether two variables point to the same location in memory).

The is and is not operators are very efficient as they simply compare the memory addresses of two objects (they do not perform any additional operations).

OperatorNameDescription
isIsVerifies if two variables point to the same object
is notIs notVerifies if two variables do not point to the same object

List of identity operators

Operator is

The is operator is used to verify if two variables refer to the same object in memory.

a = [1, 2, 3]
b = a

is_equal = (a is b)  # True, since a and b refer to the same object
Copied!

In this example, both a and b point to the same object in memory, so the expression a is b is True.

Operator is not

The is not operator is used to verify if two variables do NOT refer to the same object in memory.

x = 10
y = 10

is_not_equal = (x is not y)  # False, since x and y refer to the same object
Copied!

In this example, both x and y point to the same object in memory (in Python, small integers are stored in the same location), so the expression x is not y is False.

Usage examples