Python Membership and Identity Operators in, not in, is, is not
Membership Operator
Subscription operators are the operators used to verify the price membership. It tests for membership in the order of strings, lists or tuples.
'In' Operator:
The operator in the operator is used to check whether an order has value. Finding a variable in the specified order assumes that it is true if it is incorrect.
Lets see an Example
list1=[1,2,3,4,5,6]
list2=[7,8,9,10]
for item in list1:
if item in list2:
print("overlapping")
else:
print("not overlapping")
Output:
not overlapping
Lets see an example without in operator
def overlapping(list1,list2):
c=0
d=0
for i in list1:
c+=1
for i in list2:
d+=1
for i in range(0,c):
for j in range(0,d):
if(list1[i]==list2[j]):
return 1
return 0
list1=[1,2,3,4,5,6]
list2=[7,8,9,10]
if(overlapping(list1,list2)):
print("overlapped ")
else:
print("not overlap")
‘not in’ operator-
Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.
Lets see an Example
x = 25
y = 30
list = [10, 20, 30, 40, 50 ];
if ( x not in list ):
print("x is not present in the given list")
else:
print("x is present in the given list")
if ( y in list ):
print("y is present in the given list")
else:
print("y is not present in the given list")
Identity mode
Used to determine whether a value belongs to a particular class or type in Python. They are
commonly used to determine the type of data that contains a fixed variable.
There are various recognition operators such as
'is' Operator - If true, the variables on either side of the operator represent the same object
and otherwise false.
Lets see by an example:
x = 5
if (type(x) is int):
print("true")
else:
print("false")
Output:
true
‘is not’ operator – Evaluates to the false if the variables on either side of the operator
point to the same object and true otherwise.
Lets see by an example
x = 6.3
if (type(x) is not int):
print("true")
else:
print("false")
Output:
true
In this Article i have explained about Membership and identity operator
0 Comments
If you have any doubt, Please let me know.