Real Python: 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
inoperator in Python is a membership operator used to check if a value is part of a collection. - You can write
not inin 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 theinoperator for membership testing. - You can support
inandnot inin custom classes by implementing methods like.__contains__(),.__iter__(), or.__getitem__().
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
https://realpython.com/courses/checking-membership-in-not-in-operators/