Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

The “in” and “not in” operators in Python

Python In And Not In Operators

Hey! So today we are going to discuss the “in” and “not in” operators in Python.

Python “in” operator

Basically, the in operator in Python checks whether a specified value is a constituent element of a sequence like string, array, list, or tuple etc.

When used in a condition, the statement returns a Boolean result evaluating into either True or False. When the specified value is found inside the sequence, the statement returns True. Whereas when it is not found, we get a False.

Not let us take an example to get a better understanding of the in operator working.

#in operator working

list1= [1,2,3,4,5]
string1= "My name is AskPython"
tuple1=(11,22,33,44)

print(5 in list1) #True
print("is" in string1) #True
print(88 in tuple1) #False

Output:

Python In Output
Python in Output

Here:

Firstly, we have initialised a list list1, a string string1 and a tuple tuple1 with some values. Then we use the in operator to check whether some values are part of the above sequences or not.

As we can see from the above output, 5 in list1 evaluates into a True. Which signifies that the value 5 is found inside the list.

Similarly, using the in operator we also confirm the presence of the string “is” in string1. But for the last case, the condition results in a False since 88 is not present inside the sequence tuple1.

Python “not in” operator

The not in operator in Python works exactly the opposite way as the in operator works. It also checks the presence of a specified value inside a given sequence but it’s return values are totally opposite to that of the in operator.

When used in a condition with the specified value present inside the sequence, the statement returns False. Whereas when it is not, we get a True.

Let us take the previous example, just replacing in operator with the not in one.

#not in operator working

list1= [1,2,3,4,5]
string1= "My name is AskPython"
tuple1=(11,22,33,44)

print(5 not in list1) #False
print("is" not in string1) #False
print(88 not in tuple1) #True

Output:

Not In Output
not in Output

As expected, the resultant output is the exact opposite of what we got earlier using the in operator.

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

Previously we discussed about the working of the in and not in operator on different type of sequences. But dictionaries are not sequences. Unlike them, dictionaries are indexed on the basis of keys.

So does the above operators work on dictionaries? And if they do, how do they evaluate the condition?

Let us try to understand with an example.

#in and not in operator working on Dictionary

dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}

print("one" in dict1)
print("one" not in dict1)

print(3 in dict1)
print(3 not in dict1)

print(5 in dict1)
print(5 not in dict1)

Output:

Using In And Not In On Dictionary
Using in And not in on Dictionary

Here firstly, we have initialised a dictionary dict1 with certain set of keys and corresponding values.

As we can see from the output above, "one" in dict1 evaluates into a False. Whereas, 3 in dict1 gives us True.

So it is clear that the in operator looks for the element among the dictionary keys and not the values. Hence, similarly the last statement 5 in dict1 also results into a False as it is not a key in the dictionary.

As mentioned earlier the not in operator here too evaluates in the same manner.

Conclusion

So in this tutorial, we learned about the in and not in operators in Python, and their working with some examples.

For any further questions, feel free to use the comments below.

References