Master W3Schools Python Membership Operators In-Depth Guide

Logan Jul 13, 2026

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 membership operators explained visually
Python membership operators explained visually

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.

Python Operators Explained 🐍
Python Operators Explained 🐍

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.

🐍 Python – Membership Operators (Complete Beginner Guide)
🐍 Python – Membership Operators (Complete Beginner Guide)

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
Types of Operators in Python
Types of Operators in Python

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:

PYTHON OPERATORS
PYTHON OPERATORS

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.

🐍 Python Operators Explained | Complete Guide with Examples & Outputs
🐍 Python Operators Explained | Complete Guide with Examples & Outputs

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.

Coding For Beginners Python - Learn the Basics - Operators
Coding For Beginners Python - Learn the Basics - Operators
Python Operators Cheat Sheet
Python Operators Cheat Sheet
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Operators Explained for Beginners
Python Operators Explained for Beginners
🐍 Python Operators Cheat Sheet ✨ Learn the symbols that make Python powerful! 💻
🐍 Python Operators Cheat Sheet ✨ Learn the symbols that make Python powerful! 💻
Python bitwise operators cheat sheet
Python bitwise operators cheat sheet
Python operators code challenge infographic
Python operators code challenge infographic
Python operator precedence cheatsheet
Python operator precedence cheatsheet
10 Companies Using Python 👍
10 Companies Using Python 👍
Python operators infographic with cartoon elements
Python operators infographic with cartoon elements
an advertisement for the company's website, which is designed to look like it has been
an advertisement for the company's website, which is designed to look like it has been
Python operators cheat sheet infographic
Python operators cheat sheet infographic
7 PYTHON LIBRARIES EVERY DEVELOPER SHOULD LEARN
7 PYTHON LIBRARIES EVERY DEVELOPER SHOULD LEARN
Operators Explained for Beginners
Operators Explained for Beginners
Python Operators: The Complete Guide for Beginners
Python Operators: The Complete Guide for Beginners
Operators in Python
Operators in Python
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
Python Programming Tutorial Page, Python Programming Codes, Python Programming For Cybersecurity, Python 3.x Programming Tips, Python Java Net Programming, Python Programming Certification, Python Programming Tips For Unicode, Python Programming With Postgresql, Python Programming Advantages
Python Programming Tutorial Page, Python Programming Codes, Python Programming For Cybersecurity, Python 3.x Programming Tips, Python Java Net Programming, Python Programming Certification, Python Programming Tips For Unicode, Python Programming With Postgresql, Python Programming Advantages

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?