Introduction to Python Membership Operators
In Python, membership operators are used to test whether a value or a variable is found in a sequence or a collection. These operators return a boolean value, either True or False, depending on the result of the test. Python provides two membership operators: in and not in.
Python Membership Operator – in
The in operator checks if a value or a variable is present in a sequence or a collection. It returns True if the value is found, and False otherwise.
Let’s look at some examples:
Example 1:
fruits = ['apple', 'banana', 'orange']
if 'apple' in fruits:
print("Yes, 'apple' is present in the list of fruits.")
else:
print("No, 'apple' is not present in the list of fruits.")
Output:
Yes, 'apple' is present in the list of fruits.
Explanation: In this example, we have a list of fruits. The in operator is used to check if the value ‘apple’ is present in the list. Since ‘apple’ is indeed present in the list, the condition evaluates to True and the corresponding message is printed.
Example 2:
name = "John"
if 'o' in name:
print("Yes, 'o' is present in the name.")
else:
print("No, 'o' is not present in the name.")
Output:
Yes, 'o' is present in the name.
Explanation: In this example, we have a string variable named ‘name’. The in operator is used to check if the character ‘o’ is present in the string. Since ‘o’ is present in the name, the condition evaluates to True and the corresponding message is printed.
Python Membership Operator – not in
The not in operator checks if a value or a variable is not present in a sequence or a collection. It returns True if the value is not found, and False otherwise.
Let’s look at some examples:
Example 1:
numbers = [1, 2, 3, 4, 5]
if 6 not in numbers:
print("Yes, 6 is not present in the list of numbers.")
else:
print("No, 6 is present in the list of numbers.")
Output:
Yes, 6 is not present in the list of numbers.
Explanation: In this example, we have a list of numbers. The not in operator is used to check if the value 6 is not present in the list. Since 6 is indeed not present in the list, the condition evaluates to True and the corresponding message is printed.
Example 2:
word = "Hello"
if 'z' not in word:
print("Yes, 'z' is not present in the word.")
else:
print("No, 'z' is present in the word.")
Output:
Yes, 'z' is not present in the word.
Explanation: In this example, we have a string variable named ‘word’. The not in operator is used to check if the character ‘z’ is not present in the string. Since ‘z’ is indeed not present in the word, the condition evaluates to True and the corresponding message is printed.
Conclusion
Membership operators in Python, namely in and not in, are powerful tools for checking the presence or absence of values in sequences or collections. They provide a convenient way to perform such tests and return boolean values based on the results.
By using these operators, you can easily determine if a value or a variable is part of a list, string, tuple, or any other iterable object. This can be particularly useful in various programming scenarios, such as searching for specific elements or filtering data based on certain conditions.
Remember to use the membership operators wisely and appropriately in your Python code to ensure accurate and efficient evaluations.