After completing this chapter, you will be able to:
- Understand Membership Operators.
- Explain why membership operators are used.
- Learn the
inandnot inoperators. - Check whether an element exists in a sequence.
- Write programs using membership operators.
- Understand real-world applications.
Membership Operators are used to determine whether a particular value exists in a sequence.
Python supports membership testing for several data types such as:
- Strings
- Lists
- Tuples
- Sets
- Dictionaries
Membership operators always return either True or False.
These operators are widely used in searching, validation, authentication, and filtering operations.
A Membership Operator checks whether a specified value exists in a sequence.
Python provides two membership operators:
innot in
Membership operators help to:
- Search elements in a sequence.
- Validate user input.
- Verify usernames.
- Check product availability.
- Search records.
- Improve program readability.
| Operator | Description |
|---|---|
in |
Returns True if the value exists in the sequence |
not in |
Returns True if the value does not exist in the sequence |
The in operator returns True if the specified value is present in a sequence.
value in sequencelanguage = "Python"
print("P" in language)
print("Z" in language)True
False
The not in operator returns True if the specified value is not present in a sequence.
value not in sequencelanguage = "Python"
print("Z" not in language)
print("P" not in language)True
False
text = "Python"
print("t" in text)Output
True
numbers = [10, 20, 30, 40]
print(20 in numbers)Output
True
colors = ("Red", "Green", "Blue")
print("Green" in colors)Output
True
fruits = {"Apple", "Mango", "Orange"}
print("Apple" in fruits)Output
True
By default, membership operators check keys.
student = {
"id":101,
"name":"Rahul"
}
print("name" in student)Output
True
Write a Python program to check whether "Python" is available in the course list.
courses = ["Java", "Python", "SQL", "HTML"]
if "Python" in courses:
print("Course Available")Course Available
Write a Python program to check whether a username already exists.
users = ["admin", "manager", "student"]
username = "admin"
if username in users:
print("Username Already Exists")Username Already Exists
products = ["Laptop", "Mouse", "Keyboard"]
item = "Mouse"
if item in products:
print("Product Available")
else:
print("Product Not Available")Product Available
word = "Programming"
print("g" in word)True
Membership operators are used in:
- Login Systems
- User Authentication
- Search Applications
- Student Management Systems
- Inventory Management
- E-Commerce Websites
- Library Management Systems
- Easy element searching.
- Improves readability.
- Works with multiple sequence types.
- Returns Boolean values.
- Reduces the need for loops.
| in | not in |
|---|---|
| Returns True if value exists | Returns True if value does not exist |
| Used for presence checking | Used for absence checking |
text = "Python"
print("Py" in text)Output
True
The operator searches for substrings in strings.
student = {
"id":101,
"name":"Rahul"
}
print("Rahul" in student)Output
False
Reason:
Membership operators check keys, not values.
To check values:
print("Rahul" in student.values())Output
True
Membership operators check whether a value exists in a sequence.
innot in
Boolean (True or False).
Yes.
They check dictionary keys by default.
- Strings
- Lists
- Tuples
- Sets
- Dictionaries
in→ Checks whether a value exists.not in→ Checks whether a value does not exist.- Returns
TrueorFalse. - Works with strings, lists, tuples, sets, and dictionaries.
- Dictionary membership checks keys by default.
In this chapter, you learned:
- What Membership Operators are.
- The
inoperator. - The
not inoperator. - Membership testing with different data types.
- Practical programs.
- Real-world applications.
- Common mistakes.
- Interview questions.