Membership Operators
What are Membership Operators
These operators are used to check if a given value is a member of a sequence and returns True
if it is else it returns False
Membership Operators in Python
There are only two membership operators in Python
in
operator
in
operator checks if the value is a member of a sequence, and then returns either True
if it is a member of that given sequence else returns False
Here’s some examples
In the following examples we’ll be using interactive shell
Example
Warning: When using membership operators with strings, know that
"y"
and"Y"
do not have the same value, so trying to check if capital"Y"
is inname
where there is only small"y"
will always returnFalse
Note: Also when cheking for the existence of sub-string within a sequence, the order matters
not in
operator
not in
operator exactly does the opposite by checking if the value is not a member of a sequence, and then returns either True
if it is not a member of that given sequence else returns False
Here’s some examples