Python membership operators
Python Membership Operators
Python’s membership operators help us determine whether an item exists in a given container type object—in other words, whether an item is a member of a given container type object.
Python has two membership operators: in
and not in
. Both return a Boolean result. The result of the “in” operator is the opposite of the “not in” operator.
You can use the “in” operator to check whether a substring exists within a larger string, whether an item exists within a list or tuple, or whether a sublist or subtuple is contained within a list or tuple.
In the following example, we check whether different substrings belong to the string var = “TutorialsPoint”. Python distinguishes based on the Unicode value of the characters. Therefore, “To” is not equal to “to”. Also note that if the “in” operator returns True, the “not in” operator evaluates to False.
var = "TutorialsPoint"
a = "P"
b = "tor"
c = "in"
d = "To"
print (a, "in", var, ":", a in var)
print (b, "not in", var, ":", b not in var)
print (c, "in", var, ":", c in var)
print (d, "not in", var, ":", d not in var)
It will produce the following output –
P in TutorialsPoint : True
tor not in TutorialsPoint : False
in in TutorialsPoint : True
To not in TutorialsPoint : True
You can use the “in/not in” operators to check for membership of items in a list or tuple.
var = [10,20,30,40]
a=20
b = 10
c=a-b
d = a/2
print (a, "in", var, ":", a in var)
print (b, "not in", var, ":", b not in var)
print (c, "in", var, ":", c in var)
print (d, "not in", var, ":", d not in var)
It will produce the following output −
20 in [10, 20, 30, 40] : True
10 not in [10, 20, 30, 40] : False
10 in [10, 20, 30, 40] : True
10.0 not in [10, 20, 30, 40] : False
In the last example, “d” is a floating-point number, but it is compared to 10 (an integer) in the list, resulting in True. Even if a number is given in another format, such as binary, octal, or hexadecimal, the membership operator can tell us whether it is in the sequence.
>>> 0x14 in [10, 20, 30, 40]
True
However, if you try to check if two consecutive numbers exist in a list or tuple, the in operator returns False. If the list/tuple itself contains consecutive numbers as a sequence, then it returns True.
var = (10,20,30,40)
a=10
b = 20
print ((a,b), "in", var, ":", (a,b) in var)
var = ((10,20),30,40)
a=10
b = 20
print ((a,b), "in", var, ":", (a,b) in var)
It will produce the following output –
(10, 20) in (10, 20, 30, 40) : False
(10, 20) in ((10, 20), 30, 40) : True
Python’s membership operators also work well with set objects.
var = {10,20,30,40}
a = 10
b = 20
print (b, "in", var, ":", b in var)
var = {(10,20),30,40}
a = 10
b = 20
print ((a,b), "in", var, ":", (a,b) in var)
Will produce the following output –
20 in {40, 10, 20, 30} : True
(10, 20) in {40, 30, (10, 20)} : True
Dictionary objects can use the in and not in operators. However, Python only checks the set of keys, not the membership of values.
var = {1:10, 2:20, 3:30}
a = 2
b = 20
print (a, "in", var, ":", a in var)
print (b, "in", var, ":", b in var)
This will produce the following output –
2 in {1:10, 2:20, 3:30}: True
20 in {1:10, 2:20, 3:30}: False