Python 3 – Membership Operators Example
Python is a popular programming language used to develop web and desktop applications. It is considered one of the easiest programming languages to learn due to its simplicity and readability. In this article, we are going to discuss one of Python’s unique features–the membership operators.
A membership operator is a Python keyword that is used to test if a value or a variable is a member of a sequence. When using membership operators, it is essential to understand two types of sequences: lists and tuples. Lists are mutable, meaning they can be modified after creation, while tuples are immutable, meaning they cannot be changed once created.
Python has two membership operators: in
and not in
. They are used in conditional statements to test if a value or a variable is a member of a sequence. The in
operator returns True
if the value or variable is found in the sequence and False
if not. The not in
operator does the opposite, returning False
if the value or variable is in the sequence and True
if not.
Here are some examples that illustrate how to use Python’s membership operators:
Example 1:
# Initialize a list
numbers = [1, 2, 3, 4, 5]
# Loop through the list and check if a number is in the list
for number in range(1, 7):
if number in numbers:
print(f"{number} is in the list")
else:
print(f"{number} is not in the list")
Output:
1 is in the list
2 is in the list
3 is in the list
4 is in the list
5 is in the list
6 is not in the list
In this example, we initialize a list of numbers from 1 to 5. We then loop through the range of numbers from 1 to 7 and use the in
operator to check if each number is in the list. If the number is in the list, we print a message indicating that it is in the list. Otherwise, we print a message indicating that it is not in the list.
Example 2:
# Initialize a tuple
names = ("John", "Mike", "Sarah")
# Check if a name is in the tuple
if "John" in names:
print("John is in the tuple")
else:
print("John is not in the tuple")
if "David" not in names:
print("David is not in the tuple")
else:
print("David is in the tuple")
Output:
John is in the tuple
David is not in the tuple
In this example, we initialize a tuple of names. We then use the in
and not in
operators to check if “John” and “David” are in the tuple. If “John” is in the tuple, we print a message indicating that he is. If “David” is not in the tuple, we print a message indicating that he is not.
Example 3:
# Initialize a list of strings
fruits = ["apple", "banana", "orange", "kiwi"]
# Check if a string starts with a certain letter
for fruit in fruits:
if fruit.startswith("a"):
print(f"{fruit} starts with the letter 'a'")
else:
print(f"{fruit} does not start with the letter 'a'")
Output:
apple starts with the letter 'a'
banana does not start with the letter 'a'
orange does not start with the letter 'a'
kiwi does not start with the letter 'a'
In this example, we initialize a list of fruits. We then use the startswith()
method to check if each fruit in the list starts with the letter “a”. If the fruit starts with “a”, we print a message indicating that it does. Otherwise, we print a message indicating that it does not.
Example 4:
# Initialize a dictionary with keys and values
person = {"name": "John", "age": 30, "gender": "male"}
# Check if a key is in the dictionary
if "name" in person:
print("The 'name' key is in the dictionary")
else:
print("The 'name' key is not in the dictionary")
if "height" not in person:
print("The 'height' key is not in the dictionary")
else:
print("The 'height' key is in the dictionary")
Output:
The 'name' key is in the dictionary
The 'height' key is not in the dictionary
In this example, we initialize a dictionary of a person’s attributes. We then use the in
and not in
operators to check if “name” and “height” are keys in the dictionary. If “name” is in the dictionary, we print a message indicating that it is. If “height” is not in the dictionary, we print a message indicating that it is not.
Example 5:
# Functions can return either True or False
def is_in_list(value, lst):
return value in lst
# Call the function
result = is_in_list(5, [1, 2, 3, 4, 5])
print(result) # Output: True
Output:
True
In this example, we define a function that checks if a value is in a list using the in
operator. The function returns a boolean value, either True
or False
, indicating whether the value is in the list or not. We then call the function and print the result, which should be True
.
Conclusion
Membership operators are powerful tools in Python that allow us to test if a value or a variable is a member of a sequence. Using the in
and not in
operators, we can quickly determine if a value or a variable is present in a list, tuple, dictionary, or even a string.
By understanding how to use Python’s membership operators, we can write more efficient and robust code that saves time and effort. With this knowledge, you can start incorporating membership operators into your Python applications and make them more powerful than ever before.