Membership and Identity Operators In Python

Discover how Membership and Identity Operators in Python, like 'in', 'not in', 'is', and 'is not', work with clear examples for efficient coding.

Python, a language known for its clear syntax and readability, offers various operators that are essential for efficient and readable code. Among these, membership and identity operators are fundamental for checking relationships between values and objects. This blog delves into these operators to understand their functionality and applications.

Membership Operators

Membership operators in Python are used to test whether a value exists within a sequence, such as a list, tuple, or string. These operators include 'in' and 'not in'.

The 'in' Operator

  • Functionality: The 'in' operator checks if the specified value is present in a given sequence.
  • Return Value: It returns True if the value exists in the sequence; otherwise, it returns False.

Example.

numbers = [1, 2, 3, 4, 5]
print(3 in numbers)  # Output: True
print(6 in numbers)  # Output: False

The 'not in' Operator

  • Functionality: The 'not in' operator checks if the specified value is not present in a given sequence.
  • Return Value: It returns True if the value is not in the sequence; otherwise, it returns False.

Example.

colors = ['red', 'blue', 'green']
print('yellow' not in colors)  # Output: True
print('red' not in colors)     # Output: False

Membership operators are a concise and efficient way to check for the presence or absence of a value in a sequence in Python. They are integral in writing clean and readable code, especially when working with collections.

Identity Operators

Identity operators in Python are used to compare the memory locations of two objects. The two identity operators are 'is' and 'is not'.

The 'is' Operator

  • Functionality: The 'is' operator checks if two variables point to the same object in memory.
  • Return Value: It returns True if the variables refer to the same object; otherwise, it returns False.

Example.

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

In this example, a and b refer to the same list, so a is b is True. However, a and c are different objects, which makes a is c False.

The 'is not' Operator

  • Functionality: The 'is not' operator checks if two variables do not point to the same object in memory.
  • Return Value: It returns True if the variables refer to different objects; otherwise, it returns False.

Example.

x = ['apple', 'banana']
y = ['apple', 'banana']
z = x
print(x is not y)  # Output: True
print(x is not z)  # Output: False

Here, x and y are different objects, so x is not y is True. But x and z refer to the same object, making x is not z False.

Identity operators are essential in Python for confirming whether two variables indeed refer to the same object, which is particularly important in the context of mutable objects and optimization.

You can also check these blogs:

  1. Python Data Types