Python – Identity Operators

Python – Identity Operators Explained with Examples

In Python, identity operators are used to compare the memory locations of two objects. These operators determine if two objects refer to the same memory location, indicating that they are the same object. The two identity operators in Python are:

  • is: Returns True if the operands are the same object.
  • is not: Returns True if the operands are not the same object.

Let’s explore these operators with some examples:

Example 1: Using the “is” operator

In this example, we will compare two variables to see if they refer to the same object:

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

print(a is b)  # Output: True

Explanation: In this case, both variables “a” and “b” refer to the same memory location, which is the list [1, 2, 3]. Therefore, the “is” operator returns True.

Example 2: Using the “is not” operator

In this example, we will compare two variables to see if they refer to different objects:

x = 10
y = 20

print(x is not y)  # Output: True

Explanation: In this case, the variables “x” and “y” refer to different memory locations, as they hold different values. Therefore, the “is not” operator returns True.

Example 3: Comparing variables with None

The identity operators can also be used to compare variables with the special value None, which represents the absence of a value. Here’s an example:

name = None

print(name is None)  # Output: True

Explanation: In this case, the variable “name” is assigned the value None, which is a unique object in Python. Therefore, the “is” operator returns True.

Example 4: Comparing strings

The identity operators can also be used to compare strings. Let’s see an example:

str1 = "Hello"
str2 = "Hello"

print(str1 is str2)  # Output: True

Explanation: In this case, both variables “str1” and “str2” refer to the same memory location, as they hold the same string value. Therefore, the “is” operator returns True.

Example 5: Comparing different objects

Identity operators can be used to compare different objects as well. Here’s an example:

num = 5
str_num = "5"

print(num is str_num)  # Output: False

Explanation: In this case, the variable “num” holds an integer value, while the variable “str_num” holds a string value. Since they are different types and refer to different memory locations, the “is” operator returns False.

It is important to note that identity operators compare the memory locations of objects, not their values. Even if two objects have the same value, they may still refer to different memory locations and thus be considered different objects.

By using the identity operators “is” and “is not”, you can perform precise object comparison in Python. These operators are particularly useful when you want to check if two variables refer to the same object or if a variable is None.

Remember to use these operators with caution and only when necessary, as object comparison based on memory locations may not always be the desired behavior in every scenario.

I hope this explanation has helped you understand Python’s identity operators. Feel free to explore more examples and use cases to deepen your understanding of these operators.

Scroll to Top