Get a skill gap analysis, personalised roadmap, and AI-powered resume optimisation.
Introduction
In Python, membership operators are used to test if a value exists within a sequence, such as a list, tuple, or string. The two membership operators are in and not in. The in operator checks if an element is present, while not in checks if an element is absent. These operators return a boolean value, making them essential for conditional statements and loops in Python programming.
What are Membership Operators in Python?
The membership operator determines whether a value is present or absent in a data sequence. In other words, it tests the membership of any value in a data sequence. These data sequences can be a string, list, as well as tuples.
For example, Suppose we have a list of positive numbers starting with one and less than ten. Such that list_num = [ 1, 2, 3, 4, 5,...9] and our target value is 7. Manually it’s easy to identify in such a small list of numbers, but we would be in trouble searching from a very large data set. In such a situation, membership operators in Python come into the role.
Similar to Boolean operators, Python favours readability by employing simple English words as operators rather than potentially puzzling symbols.
We use "in" to determine whether an object is present in any sequence and use "not in" to determine whether an object is absent from any sequence.
Return Value: The return value of membership operators are boolean values(TRUE or FALSE).
Operators
Description
in Operator
One can determine whether a value exists in a sequence or not by utilising the "in" operator. It will return true if the specified variable or literal exists; otherwise, it will give false.
not in operator
One can determine whether a value does not exist in a sequence or exists by utilising the "not in" operator. It will return false if the specified variable or literal exists; otherwise, it will give false.
Let’s see the types of membership operators in detail using some examples.
In and Not In are binary operators, just like many other operators. Thus, two operands can be connected to form expressions.
Left Operand: The target value you wish to find in the set of values is the left operand.
Right Operand: The set of values where the target value might be found is the right operand.
The syntax of the membership operators in Python is as follows:
item in collection
item not in collection
Examples of Membership Operators
1. in Operator in Python
We will see various examples to understand the working of “in” operators in Python.
Example 1
Here, we will check for the target value in strings, lists, tuples, and sets.
1. Strings
We will check a character in the given string.
Code Implementation:
Python
Python
print('d' in 'Code_Studio') print('p' in 'Coding_Ninjas')
You can also try this code with Online Python Compiler
Explanation: In the above code, we get True for the first line of code because the in operator encounters the character “d” in the given string. The output for the second line of code is False since it cannot find the occurrence.
2. Lists
We will check a number in the given list.
Code Implementation:
Python
Python
value = 7 list_num = list(range(1, 10)) print(value in list_num)
You can also try this code with Online Python Compiler
Explanation: In the above code, we get True for the first print statement because the in operator encounters the string “CodingNinjas” in the given tuple. The output for the second print statement is False since it cannot find the occurrence.
4. Sets
We will check for a target value in the given sets.
Code Implementation:
Python
Python
print('Ninja' in {'CodingNinjas', 'is', 'the','best'}) print(15 in {5, 10, 15, 20, 25})
You can also try this code with Online Python Compiler
Explanation: In the above code, we get False for the first line of code because the in operator encounters the target value in the given set. The output for the second line of code is True since it can find the occurrence.
Example 2
We will see an example using in operator to find overlapping (same) values, and then we will eliminate the in operator to understand that in operator reduces the complexity.
Code Implementation:
Python
Python
listA=[2,4,6,8] listB=[5,10,15,20] for value in listA: if value in listB: print("overlapping Values") else: print("No overlapping values")
You can also try this code with Online Python Compiler
Explanation: In the above code, we are getting No overlapping values as output since the value of both the lists are different. The condition for the else statement is executed.
Now, we will update the code by eliminating the “in” operator.
Code Implementation:
Python
Python
def duplicate(listA,listB):
x =0 y =0 for i in listA: x+=1 for i in listB: y+=1 for i in range(0,x): for j in range(0,y): if(listA[i]==listB[j]): return 1 return 0
Explanation: As we can see when we remove the in operator we need to add two loops(nested loop) for the same code. Hence we can say the complexity of the code is increased.
Explanation: In the above code, we get False for the first line of code because the not in operator encounters the character “d” in the given string. The output for the second line of code is True since it cannot find the occurrence.
2. Lists
We will check a number in the given list.
Code Implementation:
Python
Python
value = 7 list_num = list(range(1, 10)) print(value not in list_num)
You can also try this code with Online Python Compiler
Explanation: In the above code, we get False for the first print statement because the not in operator encounters the string “CodingNinjas” in the given tuple. The output for the second print statement is True since it cannot find the occurrence.
4. Sets
We will check for a target value in the given sets.
Code Implementation:
Python
Python
print('Ninja' not in {'CodingNinjas', 'is', 'the','best'}) print(15 not in {5, 10, 15, 20, 25})
You can also try this code with Online Python Compiler
Explanation: In the above code, we get True for the first line of code because the not in operator encounters the target value in the given set. The output for the second line of code is False since it can find the occurrence.
Example 2
We will cover another example to see the difference in working of in and not in operators.
Code Implementation:
Python
Python
a = 4 b = 2 list_num = [3, 4, 5, 6, 7 ];
if ( a not in list_num ): print ("4 is not present in the given list") else: print ("4 is present in the given list")
if ( b in list_num ): print ("2 is present in the given list") else: print ("2 is not present in the given list")
You can also try this code with Online Python Compiler
4 is present in the given list
2 is not present in the given list
Identity Operators in Python
In addition to membership operators, Python also supports a different class of operators referred to as identity operators. Python uses identity operators to determine whether a given value belongs to a specific class or type. Identity operators are frequently used to specify the kind of data that a certain variable contains.
Python has two distinct identity operators available:
is operator
is not operator
1. is Operator
The is operator in Python returns True if both sides have the same kind. If not, False is returned.
Let's examine a Python programme to understand this better.
Example
The following example checks for the integer data type.
Code Implementation:
Python
Python
a = 3 if (type(a) is int): print ("True") else: print ("False")
You can also try this code with Online Python Compiler
Explanation: In the above code, we get True for the code since a is a member of integer class.
2. is not Operator
The is not operator in Python returns False if both sides have the same kind. If not, True is returned. It is the exact opposite of is operator in Python.
Let's examine a Python programme to understand this better.
Example
The following example checks for integer data type using is not operator.
Code Implementation:
Python
Python
a = "CN" if (type(a) is not int): print ("True") else: print ("False")
You can also try this code with Online Python Compiler
Explanation: In the above code, we get True for the code since “a” is not a member of integer class.
Frequently Asked Questions
What are member operators?
Member operators in Python (in and not in) test if a value is present or absent within a sequence like lists, strings, or tuples.
What is the use of “is operator”?
The "is" operator checks if two variables point to the same object in memory, verifying object identity rather than equality of values.
How do you check for membership in Python?
To check for membership in Python, use the in operator to verify if a value exists in a sequence, such as a list or string.
Why is membership operator used in Python?
Membership operators (in and not in) in Python are used to check if a value exists in a sequence (like a list, string, or tuple) or not, simplifying conditional checks.
What is the symbol for the membership operator?
The symbol for the membership operator in Python is "in." It is used to check if a value is present within a sequence or container.
What kinds of membership operators does Python support?
Python supports two types of membership operators. The first one is IN, and the second one is NOT IN. The IN membership operator specifies the value is present in the desired data sequence. The NOT IN membership operator specifies the value is not present in the sequence.
What will be the output of the membership operators in Python?
The membership operator determines whether a value is present or absent in a data sequence. Generally, one can use the IN or NOT IN operators. The output of both membership operators is boolean values(TRUE or FALSE).
What distinguishes a membership operator from an identity operator?
A membership operator in Python checks for the presence of a value within a sequence, such as a string, list, tuple, or set. The membership operators include in and not in.
Conclusion
In conclusion, the membership operators in Python are used to confirm any value's membership in a data set. It checks whether the value is present in a sequence, such as strings, lists, or tuples.
In this article, we saw what membership operators are. We also discussed the features and examples of membership operators in Python.
We hope this blog has helped you. We recommend you visit our articles on different topics of Python, such as: