Python Membership Operator

  • A membership operator is used to check whether the object is present or not
  • This membership operator is two types:
    • in
    •  not in

Membership in Operator

  • The membership operator returns True if a sequence with the specified value is present in the object otherwise it returns False.
Example

Membership in Operator

x = ["aman", "rohan"]

print("rohan" in x)

# returns True because a sequence with the value "rohan" is in the list
print("Amit" in x)

# returns False because a sequence with the value "Amit" is Not in the list
Output
True
False

Membership not in Operator

  • The membership operator returns True if a sequence with the specified value is not present in the object otherwise it returns False.
Example

Membership not in Operator

x = ["aman", "rohan"]

print("rohan" Not in x)

# returns True because a sequence with the value "rohan" is in the list
print("Amit" Not in x)

# returns False because a sequence with the value "Amit" is Not in the list
Output
False
True