Open In App

Python Membership and Identity Operators

Last Updated : 17 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, Membership and Identity operators help us check relationships between values and objects. They are mainly used to test whether a value exists within a sequence or whether two variables refer to same object in memory.

In this article, we will learn about Python Membership and Identity Operators.

Membership Operators

The Membership operators test for the membership of an object in a sequence, such as strings, lists or tuples. Python offers two membership operators to check or validate the membership of a value. They are as follows:

1. IN Operator

The in operator returns True if the given element exists inside a sequence, otherwise it returns False.

Example: Checking elements in different sequences

Python
list1 = [1, 2, 3, 4, 5]
str1 = "Hello World"
dict1 = {1: "Geeks", 2: "for", 3: "geeks"}

print(2 in list1)      # checking integer in a list
print('O' in str1)     # checking character in a string
print(3 in dict1)      # checking key in a dictionary

Output
True
False
True

Explanation:

  • 2 in list1: True because 2 exists in the list.
  • 'O' in str1: False because Python is case-sensitive ('O' ≠ 'o').
  • 3 in dict1: True because dictionary membership checks keys, not values.

2. NOT IN Operator

The not in operator is the opposite it returns True if the element is not found in a sequence.

Example: Using not in with list, string and dictionary

Python
list1 = [1, 2, 3, 4, 5]
str1 = "Hello World"
dict1 = {1: "Geeks", 2: "for", 3: "geeks"}

print(2 not in list1)    # integer check in list
print('O' not in str1)   # character check in string
print(3 not in dict1)    # key check in dictionary

Output
False
True
False

Explanation:

  • 2 not in list1: False because 2 exists.
  • 'O' not in str1: True because 'O' is missing.
  • 3 not in dict1: False because 3 is a key in the dictionary.

3. operator.contains() Method

Python also provides a function from the operator module called contains() that works like in.

Syntax:

operator.contains(sequence, value)

Example: Using operator.contains() with different sequences

Python
import operator

print(operator.contains([1, 2, 3, 4, 5], 2))         # list
print(operator.contains("Hello World", 'O'))         # string
print(operator.contains({1, 2, 3, 4, 5}, 6))         # set
print(operator.contains({1: "Geeks", 2:"for"}, 3))   # dictionary key
print(operator.contains((1, 2, 3, 4, 5), 9))         # tuple

Output
True
False
False
False
False

Explanation: Works the same as in, but in function form (sometimes useful in functional programming).

Identity Operators

The Identity Operators are used to compare the objects if both objects are actually of same data type and share same memory location. There are different identity operators such as:

1. IS Operator

The is operator checks if two variables point to the same object (same memory location).

Example: Comparing different objects

Python
num1 = 5
num2 = 5

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

s1 = "hello world"
s2 = "hello world"

print(num1 is num2)  # integers
print(a is b)        # lists
print(a is c)        # reference
print(s1 is s2)      # strings

Output
True
False
True
True

Explanation:

  • num1 is num2: True because small integers are cached by Python.
  • a is b: False because even though the lists look the same, they are stored at different memory locations.
  • a is c: True because c directly refers to a.
  • s1 is s2: True because Python reuses identical string objects.

2. IS NOT Operator

The opposite of is. It checks if two variables point to different objects.

Example: Checking with is not

Python
num1 = 5
num2 = 5

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

s1 = "hello world"
s2 = "hello world"

print(num1 is not num2)
print(a is not b)
print(a is not c)
print(s1 is not s2)

Output
False
True
False
False

Explanation:

  • a is not b: True because they are different objects.
  • a is not c: False because they point to the same object.

Difference Between == and is

While comparing objects in Python, users often gets confused between Equality operator and Identity operator. The equality operator is used to compare value of two variables, whereas identities operator is used to compare memory location of two variables.

Let us see the difference with the help of an example.

Example: In this code we have two lists that contains same data, we used 'is' operator and '==' operator to compare both lists.

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

print(a is b)   # identity check
print(a == b)   # value check

Output
False
True

Explanation:

  • a == b: True because the contents are the same.
  • a is b: False because they are stored as separate list objects.

Read in detail - Difference between == and is operator


Identity Comparison Operators in Python
Visit Course explore course icon
Video Thumbnail

Identity Comparison Operators in Python

Video Thumbnail

Membership Test Operators in Python

Article Tags :

Explore