In this tutorial, you will learn about the in and not in operators in Python.
What Is “in” Operator In Python?
Essentially, the in operator in Python determines whether a specified value is a constituent element of a sequence such as a string, array, list, or tuple, among other things.
When used in a condition, the statement returns a Boolean result, which can be either True or False depending on the circumstances. When the specified value is found within the sequence, the statement returns True. Otherwise, the statement returns False. In contrast, if it is not found, we receive a False.
Let’s look at an example to get a better understanding of how the in-operating system works.
#Initializing A List, String, Tuple newList = [1,2,3,"A","B"] newString = "You Are On Coduber." newTuple = (4,5,6) #Printing True Or False Using in Operator print(1 in newList) print("A" in newList) #Should Print False as D is not Present print("D" in newList) print("Coduber" in newString) #Should Print False as web is not present. print("Web" in newString) print(5 in newTuple) #Should Print False as 2 is not present print(2 in newTuple)
Output:
True
True
False
True
False
True
False

In the first place, we’ve filled in some placeholders with some values for the list newList, the string newString, and the tuple newTuple. After that, we use the in operator to determine whether or not some values are included in the above sequences.
For example, as we can see from the output above, the number 1 in newList evaluates to a True. This indicates that the value 1 can be found somewhere in the list.
We can also confirm the presence of the string “Coduber” by using them in the operator, just as we did with newString. However, in the last case, the condition returns a False because the number 2 does not appear anywhere in the sequence newTuple.
What Is “not in” Operator In Python?
In Python, the not in
operator works in the exact opposite way that the in
operator does. It checks for the presence of a specified value within a given sequence as well, but its return values are diametrically opposed to those of the in operator.
The statement returns False when used in a condition with the specified value present within the sequence. When it is not, we get a False. Take the previous example and replace the in operator with the, not in one.
#Initializing A List, String, Tuple newList = [1,2,3,"A","B"] newString = "You Are On Coduber." newTuple = (4,5,6) #Printing True Or False Using in Operator print(1 not in newList) print("A" not in newList) #Should Print True as D is not Present print("D" not in newList) print("Coduber" not in newString) #Should Print True as web is not present. print("Web" not in newString) print(5 not in newTuple) #Should Print True as 2 is not present print(2 not in newTuple)
Output:
False
False
True
False
True
False
True
As we expected, the output produced by the not in
operator is diametrically opposed to what we obtained earlier by using the out the in
operator.
How “in” And “not in” Opertors Work In Python Dictionaries
Prior to this, we talked about the differences between the operations performed by the in and not in operators on different types of sequences. Dictionary entries, on the other hand, are not sequences. In contrast to them, dictionaries are organized according to the keys they contain.
So, do the operators listed above work with dictionaries? And, if they do, how do they assess the severity of the condition? Let us try to make sense of things by using an example.
#Initializing A Dictionary newDict = {1:"A", 2:"B", 3:"C"} #This should Print True print(1 in newDict) #This should Print False print("B" in newDict) #This should Print True print(4 not in newDict.values()) #Using Conditional Statement if 3 in newDict: print("Found in Dict") if "C" not in newDict: print("Not Found") #Finding In Values of Dictionary print("C" in newDict.values())
Output:
True
False
True
Found in Dict
Not Found
First and foremost, we have created a dictionary newDict that contains a specific set of keys and their corresponding values. As we can see from the output above, the value of “B” in newDict evaluates to a False. In contrast, the number 1 in newDict gives us the answer True.
As shown in the preceding example, using the in operator directly on the dictionary variable only checks to see if the keys are present or not. Furthermore, the values are never searched or equated using the in operator.
If you want to use the in operator to see if a value exists in the dictionary, you must use newDict.values(). As this will return a list of values from the Dictionary, the in operator will search through this list for the given value.
Wrap Up
It’s time to wrap up this tutorial with a look at some examples of the in and not in operators in Python and how they work. Let us know if you have further questions in the comments section below.
If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.
Further Read: