Python: Membership Operators

Membership Operators test for membership in a sequence, such as strings, lists, or tuples but in dictionary we can only test for presence of key, not the value.

OperatorDescriptionSyntax
inIf a sequence with the specified value is present in the object. Returns True.a in b
not inIf a sequence with the specified value is not present in the object. Returns True.a not in b

Example:

a = "ProgramsBuzz"
print("g" in a)
print("d" not in a)

Output:

True
True
Tags