Nearby lessons

36 of 159

Python - Membership Operators

📌 What You Will Learn
  • Define membership operators and what they check
  • Use the in operator to test for presence
  • Use the not in operator to test for absence
  • Apply membership operators to strings, lists, tuples, sets, and dictionaries
  • Explain how membership works for dictionary keys

Introduction

Membership operators are used to check whether a value is present in a sequence or not.

The result of a membership operation is always a Boolean value:

  • True
  • False

Membership Operators List

Operator Meaning
in Returns True if the value is present.
not in Returns True if the value is not present.

Supported Data Types

Membership operators can be used with:

  • Strings
  • Lists
  • Tuples
  • Sets
  • Dictionaries

1. in Operator

The in operator checks whether a value exists in a sequence.

Example - in Operator with String

🐍Code Cell
1s = "python"
2 
3print('p' in s)
4print('z' in s)
Output
True
False

Explanation

The character 'p' is present in the string, so the result is True.

The character 'z' is not present, so the result is False.

Example - in Operator with List

🐍Code Cell
1l = [10, 20, 30, 40]
2 
3print(20 in l)
4print(100 in l)
Output
True
False

Example - in Operator with Tuple

🐍Code Cell
1t = (10, 20, 30)
2 
3print(10 in t)
4print(50 in t)
Output
True
False

Example - in Operator with Set

🐍Code Cell
1s = {10, 20, 30}
2 
3print(20 in s)
4print(99 in s)
Output
True
False

Example - in Operator with Dictionary

For dictionaries, membership operators check keys only.

Example - Dictionary Membership

🐍Code Cell
1d = {
2 100: "durga",
3 200: "ravi"
4}
5 
6print(100 in d)
7print("durga" in d)
Output
True
False

Explanation

Key Value
100 durga
200 ravi

The value 100 is a key, so the result is True.

The value "durga" is not a key; it is a value, so the result is False.

2. not in Operator

The not in operator checks whether a value is absent from a sequence.

Example - not in with String

🐍Code Cell
1s = "python"
2 
3print('z' not in s)
4print('p' not in s)
Output
True
False

Example - not in with List

🐍Code Cell
1l = [10, 20, 30]
2 
3print(50 not in l)
4print(20 not in l)
Output
True
False

Example - not in with Tuple

🐍Code Cell
1t = (10, 20, 30)
2 
3print(100 not in t)
Output
True

Example - not in with Set

🐍Code Cell
1s = {1, 2, 3}
2 
3print(5 not in s)
Output
True

Example - not in with Dictionary

🐍Code Cell
1d = {
2 1: "python",
3 2: "java"
4}
5 
6print(3 not in d)
7print("python" not in d)
Output
True
True

Why is the Second Output True?

Dictionary membership operators check only keys.

Since "python" is a value and not a key, the result is True.

Membership Operators with User Input

🐍Code Cell
1name = input("Enter Name: ")
2 
3print('a' in name)
Output
Enter Name: Rahul
True

Using Membership Operators in Conditions

🐍Code Cell
1language = "python"
2 
3if 'p' in language:
4 print("Character Found")
Output
Character Found

Example - Using not in

🐍Code Cell
1blocked_users = ["ram", "shyam"]
2 
3user = "mohan"
4 
5if user not in blocked_users:
6 print("Access Allowed")
Output
Access Allowed

Membership Operators with Strings

For strings, membership operators check whether a substring exists.

Example - String Membership

🐍Code Cell
1s = "python programming"
2 
3print("python" in s)
4print("java" in s)
Output
True
False

Real World Usage

Membership operators are commonly used in:

  • Login systems
  • Search operations
  • Validation
  • Filtering
  • Permission checking

Example Areas

Usage Example
Login Validation Username check
Search Word search
Access Control Permission validation

Difference Between in and not in

Operator Meaning
in Checks whether a value exists.
not in Checks whether a value does not exist.
📝 Key Takeaways
  • Membership operators check whether a value exists in a sequence
  • in returns True when the value is present
  • not in returns True when the value is absent
  • The result of a membership operation is always True or False
  • Dictionary membership operators check keys only
  • For strings, membership operators check whether a substring exists

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8