Membership operators are used to determine whether the iterator or the value given is present in the sequence. Such operator types often return values in either the TRUE or FALSE format. If the value is contained in a set, the operator returns TRUE, otherwise it returns FALSE.
Membership Operators in Python
Membership operators in Python are used to test whether a value or element is a member of a sequence, such as a string, list, tuple, or set. These operators return True or False based on the presence or absence of the specified value within the sequence. All Membership operators in python are discussed below in detail with their syntax and proper example:
1. In
It is used check whether a variable is present in a sequence or not. It returns True if variable is found else it returns False.
Syntax
Variable in Sequence
Example
>>> a = [1,(1,2),"Prep","Insta"]
>>> b = (1,2)
>>> 1 in a
True
>>> b in a
True
>>> 'a' in 'PrepInsta'
True
2. Not in
It is used check whether a variable is present in a sequence or not. It returns True if variable is not found else it returns False.
Syntax
Variable not in Sequence
Example
>>> a = [1,(1,2),"Prep","Insta"]
>>> b = (1,2,3)
>>> 2 not in a
True
>>> b not in a
True
>>> 'A' not in 'PrepInsta'
True
Login/Signup to comment