Python "not in" Operator with Example

In Python 'not in' membership operator evaluates to true if it does not finds a variable in the specified sequence and false otherwise.

To check if some element is not in the sequence, a list, string, tuple, or set, use the not in operator. The not in operator is the exact opposite of in operator.

Python Not In Operator

The not in is a built-in Python operator that checks the presence of a specified value inside the given sequence, but its values are opposite to that of the in operator.

The not in operator returns a boolean value as an output. It returns either True or False.

listA = [11, 21, 29, 46, 19]
stringA = "Hello! This is Dev"
tupleA = (11, 22, 33, 44)

print(19 not in listA)
print("is" not in stringA)
print(55 not in tupleA)

Output

False
False
True

Working of “in” and “not in” Operators in Dictionaries

Dictionaries are not sequences because dictionaries are indexed based on keys. Let’s see how to work with, not in operator in dictionaries? And if they do, how do they evaluate the condition?

Let us try to understand with an example.

dict1 = {11: "eleven", 21: "twenty one", 46: "fourty six", 10: "ten"}

print("eleven" in dict1)
print("eleven" not in dict1)

print(21 in dict1)
print(21 not in dict1)

print(10 in dict1)
print(10 not in dict1)

Output

False
True
True
False
True
False

That’s it for this tutorial.

 

3.10 GEEK