Loading video player…

Checking for Membership Using "in" and "not in" Operators (Overview)

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__().
Download

Course Slides (.pdf)

9.3 MB
Download

Sample Code (.zip)

2.1 KB

00:00 Hello and welcome to this Real Python course on Python’s membership operators in and not in. My name is Martin, and I’m going to guide you through this course.

00:10 I actually have another different favorite name for this course, which would be, “What’s in a Name?” or more precisely “What’s In a Container?”, or actually, even more precisely, “What’s in a Container or an Iterator?” But let’s go there in just a little bit for the sake of this introduction.

00:27 So let’s start off by thinking about why membership operators matter. They allow you to check if an item exists inside of a container or an iterator, and that allows you to do a couple of things in programming to give you just a few real-life examples.

00:41 You may want to use it to verify user access or permissions, to validate user input against expected values, to make sure that your users don’t input anything that breaks your app, for example, to check if a file exists in a directory, to detect duplicate items when you are running a data processing pipeline.

00:58 And we could continue this. There’s a lot of use cases for Python’s membership operators. Another thing to mention is that Python’s membership operators in and not in allow you to build this type of logic in a clean and readable fashion.

01:12 So you may often use it for running some sort of conditional checks. Okay, so they’re important. Let’s get an additional voice from the past,

01:21 how Shakespeare may conclude, “Verily, they matter.” In this course, you’ll learn about Python’s in and not in operators for membership checks, how they work with common data types.

01:32 I’m not going to talk about all the data types they work with because there’s a lot of them. Then, you’ll do a bit of practical coding by building an e-commerce scenario for a very, very small Victorian business owner.

01:43 You look at some pitfalls when you run membership checks with generators, and you’ll also get to implement a membership check in custom classes using one of the dunder methods that allow you to do that.

01:56 And there will be a little bit more Shakespeare in there, just for fun. So if this sounds fun and useful, then stick with me. In the next lesson, we’ll look over the basics of the in and not in membership operators.

Become a Member to join the conversation.