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.
in operatorTh 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.
not in operatorThe 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.
in operatorlist1=[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")
check to store the overlapping or non-overlapping status. Initially, we assign the check variable a value of 0.for loop with the in operator to check for an overlapping of both the given lists. If the in operator returns true, then the check variable is assigned a value of 1. Later on, the if statement looks at the value of the check variable and if it is equal to 1, the overlapping is printed. Otherwise, if the value of the check variable is not equal to 1, then the not overlapping is printed. not in operatora = 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")
a and b, and assign them values. In addition to this, we declare a list that contains numerous values.not in operator checks separately if a and b are not present in the list. The output is then printed accordingly.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.
is operatorThe is operator returns true if the variables on either side of the operator point to the same object. Otherwise, it returns false.
is not operatorThe is not operator returns false if the variables on either side of the operator point to the same object. Otherwise, it returns true.
is operatorx = 'Educative'if (type(x) is str):print("true")else:print("false")
is operator checks if the variables on either side of the operator point to the same object or not.is not operatorx = 6.3if (type(x) is not float):print("true")else:print("false")
x and check if it is not of a float type. Then, we print true/false accordingly. In this case, the variable x is of float type, so false is returned.