Python membership and identity operators
Membership operators
Membership operators are used to identify the membership of a value. These operators test for membership in an order, that is, string, lists, or tuples. The in and not in operators are examples of membership operators.
The in operator
Th in operator is used to check whether a value is present in a sequence or not. If the value is present, then true is returned. Otherwise, false is returned.
The not in operator
The not in operator is opposite to the in operator. If a value is not present in a sequence, it returns true. Otherwise, it returns false.
Code example of the in operator
list1=[0,2,4,6,8]list2=[1,3,5,7,9]#Initially we assign 0 to not overlappingcheck=0for item in list1:if item in list2:#Overlapping true so check is assigned 1check=1if check==1:print("overlapping")else:print("not overlapping")
Explanation
- Lines 1–4: We declare and assign values to two lists. We also declare a variable named
checkto store the overlapping or non-overlapping status. Initially, we assign thecheckvariable a value of 0. - Lines 5–14: We declare a
forloop with theinoperator to check for an overlapping of both the given lists. If theinoperator returns true, then thecheckvariable is assigned a value of 1. Later on, theifstatement looks at the value of thecheckvariable and if it is equal to 1, the overlapping is printed. Otherwise, if the value of thecheckvariable is not equal to 1, then the not overlapping is printed.
Code example of the not in operator
a = 70b = 20list = [10, 30, 50, 70, 90 ];if ( a not in list ):print("a is NOT in given list")else:print("a is in given list")if ( b not in list ):print("b is NOT present in given list")else:print("b is in given list")
Explanation
- Lines 1–3: We declare two variables,
aandb, and assign them values. In addition to this, we declare a list that contains numerous values. - Lines 5–13: The
not inoperator checks separately ifaandbare not present in the list. The output is then printed accordingly.
Identity operators
Identity operators evaluate whether the value being given is of a specific type or class. These operators are commonly used to match the data type of a variable. Examples of identity operators are the is and is not operators.
The is operator
The is operator returns true if the variables on either side of the operator point to the same object. Otherwise, it returns false.
The is not operator
The is not operator returns false if the variables on either side of the operator point to the same object. Otherwise, it returns true.
Code example for the is operator
x = 'Educative'if (type(x) is str):print("true")else:print("false")
Explanation
- Line 1: We assign a string to x.
- Lines 2–5: The
isoperator checks if the variables on either side of the operator point to the same object or not.
Code example for the is not operator
x = 6.3if (type(x) is not float):print("true")else:print("false")
Explanation
- Lines 1–5: We assign a float value to the variable
xand check if itis notof a float type. Then, we printtrue/falseaccordingly. In this case, the variablexis offloattype, sofalseis returned.