Python tutorials > Core Python Fundamentals > Operators and Expressions > What are membership operators?
What are membership operators?
in and not in. Understanding and utilizing these operators effectively enhances code readability and simplifies common search operations.
Introduction to Membership Operators
in operator returns True if a value is found in the sequence; otherwise, it returns False. The not in operator does the opposite, returning True if a value is not found in the sequence and False if it is found.
Using 'in' with Strings
in operator to check if a substring exists within a string. The first print statement checks if "World" is in the string "Hello, World!", which is true. The second print statement checks for "Python", which is not present, so it returns False.
my_string = "Hello, World!"
print("World" in my_string) # Output: True
print("Python" in my_string) # Output: False
Using 'in' with Lists
in operator to check if a number exists in a list. The first example checks for 3, which is in the list, and returns True. The second example checks for 6, which is not in the list, and returns False.
my_list = [1, 2, 3, 4, 5]
print(3 in my_list) # Output: True
print(6 in my_list) # Output: False
Using 'in' with Tuples
in operator with tuples. Similar to lists, it checks if a specific element is present. 20 is in the tuple, resulting in True. 60 is not, so it returns False.
my_tuple = (10, 20, 30, 40, 50)
print(20 in my_tuple) # Output: True
print(60 in my_tuple) # Output: False
Using 'in' with Sets
in operator. The code checks for the presence of elements within the set. The behavior is identical to lists and tuples, but sets offer faster membership testing due to their underlying hash table implementation.
my_set = {1, 2, 3, 4, 5}
print(3 in my_set) # Output: True
print(6 in my_set) # Output: False
Using 'not in' Operator
not in operator is the logical inverse of in. It returns True if the element is not found in the sequence and False otherwise. In this example, 3 is in the list, so 3 not in my_list evaluates to False. 6 is not in the list, so 6 not in my_list evaluates to True.
my_list = [1, 2, 3, 4, 5]
print(3 not in my_list) # Output: False
print(6 not in my_list) # Output: True
Real-Life Use Case: Checking User Permissions
in operator, you can easily determine if a user has a specific role and grant or deny access accordingly. This enhances security and access control.
user_roles = ["admin", "editor", "viewer"]
user = "john"
if "admin" in user_roles:
print(f"{user} has admin access.")
else:
print(f"{user} does not have admin access.")
Best Practices
in and not in operators enhance code readability compared to manual looping and comparison.
When to use them
Memory footprint
Alternatives
my_list = [1, 2, 3, 4, 5]
found = False
for item in my_list:
if item == 3:
found = True
break
print(found)
Pros
Cons
FAQ
-
Can I use membership operators with dictionaries?
Yes, you can use theinoperator with dictionaries to check if a key exists in the dictionary. -
Are membership operators case-sensitive when used with strings?
Yes, membership operators are case-sensitive. "World" is different from "world". -
How do membership operators work internally?
Theinoperator iterates through the sequence and compares each element to the target value until a match is found or the end of the sequence is reached. Sets utilize hash tables, providing near constant-time membership checks.