Introduction to Python Membership Operators
Membership operators are used to check if a variable is present in the sequence like string, list or tuple.
Note: It will work only for sequences, if you try to make it work for data types like integers, it will not work.
Operator | Example | Description |
---|---|---|
in | a in b | Returns True if the variable is present in the sequence, else False. |
not in | a not in b | Returns True if the variable is not present in the sequence, else False. |
Membership Operators
in Operator
in Operator returns True if the specified variable or value is present in the sequence. It can be any sequence like a string, list, or tuple. If the value is not present in the sequence, then it will return False.
In [1]:
name = "logical"
print("logi" in name)
In [2]:
name = ["Logical", "Python"]
print("Logical" in name)
In [3]:
name = ["Logical", "Python"]
print("Learn" in name)
not in Operator
not in Operator returns True if the specified variable or value is not present in the sequence. If the value is present in the sequence, then it will return True.
In [1]:
name = "logical"
print("logi" not in name)
In [2]:
name = ["Logical", "Python"]
print("Logical" not in name)
In [3]:
name = ["Logical", "Python"]
print("Learn" not in name)
Resources
Handwritten Notes