Membership Operators

Python · Operators

Theory

in and not in test whether a value is contained in a collection and return True or False. 'Mumbai' in cities checks every list element with ==. On strings, in looks for a substring, so 'umb' in 'Mumbai' is True. On a dictionary, in checks the keys, not the values: 'name' in employee is True but 'Asha' in employee is False; use 'Asha' in employee.values() for values.

Speed depends on the container. A list or tuple is scanned item by item, so the time grows with its length (O(n)). A set or dict hashes the value and jumps straight to where it would be stored, which takes roughly constant time no matter how big it is (average O(1)). That is why converting a big list of IDs to a set before many lookups can make code dramatically faster. in raises TypeError if the right side is not a container (5 in 10) or if you search a set for an unhashable value like a list.

Why we use it

Checking 'is this item already there?' is everywhere: valid status codes, blocked users, duplicate IDs, allowed file types. in expresses it in one readable line without writing a loop.

When to use it

Use in for any containment check. For many lookups against the same large collection, store it as a set or dict first. Use not in for exclusion checks; it reads better than not (x in y).

How it works

  1. Evaluate 'Mumbai' in cities
  2. Is cities a set or dict? Yes: Hash 'Mumbai' and check that slot directly. No: Scan items one by one from the start.
  3. Did some item compare equal? Yes: Return True and stop scanning. No: Return False after the last item.
  4. print shows True

Syntax

value in sequence
value not in sequence

Example

cities = ['Mumbai', 'Delhi']
print('Mumbai' in cities)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic