Membership Operators
Membership operators are operators used to validate the membership of a value. It test for membership in a sequence, such as strings, lists, or tuples.
- in operator : The ‘in’ operator is used to check if a value exists in a sequence or not. Evaluates to true if it finds a variable in the specified sequence and false otherwise.
# Python program to illustrate# Finding common member in list# using 'in' operatorlist1=[1,2,3,4,5]list2=[6,7,8,9]foriteminlist1:ifiteminlist2:print("overlapping")else:print("not overlapping")chevron_rightfilter_noneOutput:
not overlapping
Same example without using in operator:
# Python program to illustrate# Finding common member in list# without using 'in' operator# Define a function() that takes two listsdefoverlapping(list1,list2):c=0d=0foriinlist1:c+=1foriinlist2:d+=1foriinrange(0,c):forjinrange(0,d):if(list1[i]==list2[j]):return1return0list1=[1,2,3,4,5]list2=[6,7,8,9]if(overlapping(list1,list2)):print("overlapping")else:print("not overlapping")chevron_rightfilter_noneOutput:
not overlapping
-
‘not in’ operator- Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.
# Python program to illustrate# not 'in' operatorx=24y=20list=[10,20,30,40,50];if( xnotinlist):print("x is NOT present in given list")else:print("x is present in given list")if( yinlist):print("y is present in given list")else:print("y is NOT present in given list")chevron_rightfilter_none
Identity operators
In Python are used to determine whether a value is of a certain class or type. They are usually used to determine the type of data a certain variable contains.
There are different identity operators such as
-
‘is’ operator – Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
# Python program to illustrate the use# of 'is' identity operatorx=5if(type(x)isint):print("true")else:print("false")chevron_rightfilter_noneOutput:
true
- ‘is not’ operator – Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.
# Python program to illustrate the# use of 'is not' identity operatorx=5.2if(type(x)isnotint):print("true")else:print("false")chevron_rightfilter_noneOutput:
true
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python - tensorflow.identity()
- numpy.identity() in Python
- numpy matrix operations | identity() function
- G-Fact 19 (Logical and Bitwise Not Operators on Boolean)
- Increment and Decrement Operators in Python
- Python | Solve given list containing numbers and arithmetic operators
- Merging and Updating Dictionary Operators in Python 3.9
- Precedence and Associativity of Operators in Python
- Python Operators for Sets and Dictionaries
- Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...)
- Inplace Operators in Python | Set 2 (ixor(), iand(), ipow(),…)
- Inplace vs Standard Operators in Python
- Chaining comparison operators in Python
- Python | Splitting operators in String
- Python Logical Operators with Examples
- Python Bitwise Operators
- Python 3 - Logical Operators
- How To Do Math in Python 3 with Operators?
- Python Arithmetic Operators
- Assignment Operators in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

