not in - Membership Operator
not in Operator
not in Membership Operator is used to check if specific value is not present in given collection.
Syntax
The syntax of not in Operator with the operands is
x not in collection
where x
is a value, and collection
is any collection of items like a list, tuple, set, string, etc.
not in operator takes two operands as shown in the above expression. Left operand is the value/element, and right operand is the collection.
not in operator returns a boolean value. If the element or value x
is not present in the collection
, then the above expression returns a of True, or else the expression returns False.
Example
In this example, we take a list of strings, and check if the value "guava"
is not present in this list, using not in keyword.
Python Program
fruits = ['apple', 'banana', 'cherry', 'mango']
x = 'guava'
if x not in fruits:
print(f'{x} is not in the given list.')
else:
print(f'{x} is in the given list.')
Output
guava is not in the given list.
Summary
In this tutorial of Python Examples, we learned about not in Membership Operator, and how to use it to check if specific element is not present in a given collection.