PYTHON MEMBERSHIP OPERATORS
INTRODUCTION
PYTHON MEMBERSHIP OPERATORS check if any value is the member of a list or not.
If its found the given number in the list then it returns TRUE. Otherwise returns FALSE.
There are two MEMBERSHIP OPERATORS are available in PYTHON.
1. IN OPERATOR
2. NOT IN OPERATOR
IN OPERATOR
LIST EXAMPLE FOR POSITIVE CASE
# Membership operator example
a = 9
b = [2, 9, 18]
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\na is a member of b :", a in b)
PYTHON MEMBERSHIP OPERATORS : Output
Value of a -> 9
Value of b -> [2, 9, 18]
a is a member of b : True
SET EXAMPLE FOR POSITIVE CASE
# Membership operator example
a = 9
b = (2, 9, 18)
print("\nValue of a -> ", a)
print("\nValue of b -> ", b)
print("\na is a member of b :", a in b)
PYTHON MEMBERSHIP OPERATORS : Output
Value of a -> 9
Value of b -> (2, 9, 18)
a is a member of b : True
In the above example we have assigned 9 to variable a. We have defined a list and assigned to b which includes 9.
Now we are excluding 9 from the list to see the impact.
LIST EXAMPLE FOR NEGATIVE CASE
# Membership operator example
a = 9
b = [2, 10, 18]
print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\na is a member of b :", a in b)
PYTHON MEMBERSHIP OPERATORS : Output
Value of a -> 9
Value of b -> [2, 10, 18]
a is a member of b : False
SET EXAMPLE FOR NEGATIVE CASE
# Membership operator example
a = 9
b = (2, 10, 18)
print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\na is a member of b :", a in b)
PYTHON MEMBERSHIP OPERATORS : Output
Value of a -> 9
Value of b -> (2, 10, 18)
a is a member of b : False
Since 9 is excluded from the list the result is FALSE.
NOT IN OPERATOR
NOT IN OPERATOR returns TRUE if a number is not a member of a list.
LIST EXAMPLE FOR POSITIVE CASE
# Membership operator example
a = 9
b = [2, 10, 18]
print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\na is not a member of b :", a not in b)
PYTHON MEMBERSHIP OPERATORS : Output
Value of a -> 9
Value of b -> [2, 10, 18]
a is not a member of b : True
SET EXAMPLE FOR POSITIVE CASE
# Membership operator example
a = 9
b = (2, 10, 18)
print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\na is not a member of b :", a not in b)
PYTHON MEMBERSHIP OPERATORS : Output
Value of a -> 9
Value of b -> (2, 10, 18)
a is not a member of b : True
If we include 9 in the list then PYTHON will return FALSE.
LIST EXAMPLE FOR NEGATIVE CASE
# Membership operator example
a = 9
b = [2, 9, 18]
print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\na is not a member of b :", a not in b)
PYTHON MEMBERSHIP OPERATORS : Output
Value of a -> 9
Value of b -> [2, 9, 18]
a is not a member of b : False
SET EXAMPLE FOR NEGATIVE CASE
# Membership operator example
a = 9
b = (2, 9, 18)
print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\na is not a member of b :", a not in b)
PYTHON MEMBERSHIP OPERATORS : Output

RELATED TOPICS:
Pingback: PYTHON LOGICAL OPERATORS - Sayantan's Blog On Python Programming