Python Membership operator


The meaning of a Membership Operator in Python:

It can be explained as being an operator that is adopted to confirm the membership of a value. The membership operator is practised to test memberships in variables such as strings, integers, and tuples.

The membership operators are of two types.

  • in operator
  • not in operator

The membership operator checks for the sequence of data's appearance into different sequences or structures and verifies the same.
The operators are practised to conclude if there are two forms of value as a piece of a sequence, like string or list membership operators: in operator and not in operator. 

              in

 Returns True if a sequence with the specified value is present in the specified variable.

            not in

  Returns True if a sequence with the specified value is not present in the specified variable.


Unlike other programming languages, C and Java do not have membership operators.

Example of in and not in operators are:

  • in operator
  • When x in y, then the outcome of in is 1 if x is a member of sequence y.
  • In the program, we search for the data sequence values (10,20) in the list. If the value is found, it prints the resultant statement.
  • not in operator
  • When x is not in y, the outcome of not in gives a 1 if x is not a member of sequence y.
  • In the program, if the data value' 32' is not present in the list, it returns false and reprints the statement after the if condition.

Example:

#not in x = ["knowledge2life 1", "knowledge2life 2"] print("knowledge2life 2" not in x) #in x = ["knowledge2life 1", "knowledge2life"] print("knowledge2life" in x) }

OUTPUT:

False
True