Python - Membership Operators
- Membership operator is used to test whether the variable or value is present in the sequence or not.
- The sequence can be a string, a tuple, a list, or a dictionary. In the case of the dictionary, we can only test for the occurrence of a key, not its value.
Python has two membership operators.
Operators | Meeaning |
---|---|
in | Returns True only if the variable or value is occurred in the given sequence, Otherwise returns False. |
not in | Returns True only if the variable or value is not occurred in the given sequence, otherwise returns False. |
Example with Membership Operators
>> z='welcome'
>> a='w'
>> print(a in z)
True
>> b='come'
>> print(b not in z)
False
>> print(b in z)
True
>> print(a not in z)
False