Welcome to our in-depth guide on Python's membership operators! If you're learning Python or seeking to enhance your understanding of its operational functionalities, you've come to the right place. Python membership operators are integral to comparing values and sets, enabling us to check if a certain value or item belongs to a range, string, list, or another sequence. Let's dive in!

Python provides robust opportunities to explore membership operations through three powerful operators: in, not in, and the newer addition, __contains__() method. Each offers unique functionalities that broaden your coding capabilities. Let's break down each, starting with the most fundamental.

In and Not In Operators
The in and not in operators simplify the process of checking membership. These operators work with lists, arrays, tuples and dictionaries, and even strings.

Use Cases
- To verify if an item exists in a collection, e.g.,
x in y - To confirm the absence of an item in a collection, e.g.,
x not in y

Different Types of Collections
The in and not in operators function differently depending on the collection type. While in lists and tuples they check for object existence at a specific index, in strings, they verify the substring's presence anywhere within the main string.
For instance: in operator when applied to a list checks if the value is in the list; when applied to a string, it confirms the substring's presence somewhere in the main string:

fruit = 'apple'
print('app' in fruit) # Returns: True
The __contains__() Method
Introduced in Python 3.8, the __contains__() method is designed to optimize lookups in sequences where __contains__() is implemented, like dictionaries and sets. With this new method, Python can now directly call .__contains__(item) instead of iterating through elements for membership checks.

Performance Enhancement
The introduction of the __contains__() method improves performance by replacing the traditional iteration process with targeted object function calls. This is particularly beneficial for large data sets.



















Usage
- To check for membership using the __contains__() method:
item in sequence._._contains__()
days = {'Monday': 1, 'Tuesday': 2}
print('Monday' in days.__contains__()) # Returns: True
Python's membership operators offer a versatile range of functionalities that target various types of collections. From checking presence to absence, and from lists to dictionaries, these operators and methods provide an effective means to streamline your coding process. As Python continues to evolve, embracing new methods like __contains__() expands your coding horizons.
Continuous learning and practice are the keys to mastery. So, go ahead, bolster your Python experiences, and explore more examples of membership operators to apply your newfound knowledge. Who knows where this journey will take you next?