VOOZH about

URL: https://www.geeksforgeeks.org/python/python-membership-identity-operators-not-not/

⇱ Python Membership and Identity Operators - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Membership and Identity Operators

Last Updated : 9 Mar, 2026

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.

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.


Output
True
False
True

Explanation:

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

2. NOT IN Operator

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


Output
False
True
False

Explanation:

  • 2 not in l: False because 2 exists.
  • 'O' not in s: True because 'O' is missing.
  • 3 not in d: 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


Output
True
False
False
False
False

Explanation: Works the same as in, but in function form (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).


Output
True
False
True
True

Explanation:

  • n1 is n2: 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 "is not" operator checks if two variables point to different objects.


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

The equality operator (==) is used to compare value of two variables, whereas identity operator (is) is used to compare memory location of two variables.

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


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.

Mutable objects (list, dict, set) always create new objects, so "is" operator between identical-looking containers always returns False.
Immutable objects (str, tuple, small integers) may reuse memory, so "is" can sometimes return True depending on Python's implementation.

Related Articles:

Difference between == and is operator

Comment
Article Tags: