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 overlapping
check=0
for item in list1:
if item in list2:
#Overlapping true so check is assigned 1
check=1
if 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 check to store the overlapping or non-overlapping status. Initially, we assign the check variable a value of 0.
  • Lines 5–14: We declare a 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.

Code example of the not in operator

a = 70
b = 20
list = [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, a and b, and assign them values. In addition to this, we declare a list that contains numerous values.
  • Lines 5–13: The not in operator checks separately if a and b are 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 is operator 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.3
if (type(x) is not float):
print("true")
else:
print("false")

Explanation

  • Lines 1–5: We assign a float value to the variable 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.

Free Resources