Python Membership Operators

python-membership-operators

Membership Operators in Python.

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.

 

 

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