Checking for Membership Using Python's "in" and "not in" Operators

Python’s in and not in operators allow you to quickly check if a given value is or isn’t part of a collection of values. This type of check is generally known as a membership test in Python. Therefore, these operators are known as membership operators.

By the end of this video course, you’ll understand that:

  • The in operator in Python is a membership operator used to check if a value is part of a collection.
  • You can write not in in Python to check if a value is absent from a collection.
  • Python’s membership operators work with several data types like lists, tuples, ranges, and dictionaries.
  • You can use operator.contains() as a function equivalent to the in operator for membership testing.
  • You can support in and not in in custom classes by implementing methods like .__contains__(), .__iter__(), or .__getitem__().

What’s Included:

Downloadable Resources:

Related Learning Paths:

About Martin Breuss

Martin is Real Python's Head of Content Strategy. With a background in education, he's worked as a coding mentor, code reviewer, curriculum developer, bootcamp instructor, and instructional designer.

» More about Martin

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

← Browse All Courses

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy