Membership Operators in Python Tutorial

In this section, we will learn what membership operators are and how to use them in Python.

Note: we’re assuming you already familiar with operators in general.

What is Membership Operator in Python?

In Python, we can create objects that can store multiple values in one object. For example, list and tuple data types are just two examples of objects that can store multiple values.

For objects of type list, tuple etc. where we have multiple values stored in one object, we can use the membership operators to see if a specific element is stored (is a member) in that object or not.

List of Membership Operator in Python

Here’s the list of membership operators in Python:

Operator Description
in Using this operator, we can check to see if an object contains a value or not.
no in Using this operator, we can check to see if an object does not contain a value!

Python in Operator

The `in` operator is used to see if a value is in an object or not.

If the target value was in the target object, then the return value of this operator will be True.

Otherwise the value False will return.

Python in Operator Syntax

value in object

Example: using Python in operator

nameList = ["Jack","Ellen","Omid","John"]
if "Jack" in nameList:
    print("The nameList has the value Jack as one of its elements")

Output:

The nameList has the value Jack as one of its elements

Python not in Operator

The `not in` operator is used to see if a value is not in an object.

If the target value was not in the target object, then the return value of this operator will be True.

Otherwise the value False will return.

Python not in Operator Syntax

value in object

Example: using Python not in Operator

nameList = ["Jack","Ellen","Omid","John"]
if "Elon" not in nameList:
    print("The nameList does not have the value Elon as part of its elements")

Output:

The nameList does not have the value Elon as part of its elements
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies