How do you use in in python?
Hey! So today we are going to discuss the “in” and “not in” operators in Python. Basically, the When used in a condition, the statement returns a Boolean result evaluating into either Not let us take an example to get a better
understanding of the Output: Python in OutputHere: Firstly, we have initialised a list As we can see from the above output, Similarly, using the Python “not in” operatorThe When used in a condition with
the specified value present inside the sequence, the statement returns Let us take the previous example, just replacing #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: As
expected, the resultant output is the exact opposite of what we got earlier using the Working of “in” and “not in” Operators in Python DictionariesPreviously we discussed about the working of the 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 DictionaryHere firstly, we have initialised a dictionary As we can see from the output above, 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 As mentioned earlier the ConclusionSo in this tutorial, we learned about the For any further questions, feel free to use the comments below. References
How does the in keyword work Python?The in keyword has two purposes: To check if a value is present in a list, tuple, range, string, etc. To iterate through a sequence in a for loop.
What does the IN operator do for lists?The in operator is then used to determine whether or not some values are part of the previous sequence. As we can see from the output above, "TutorialsPoint" in the list evaluates to True. This indicates that the value "TutorialsPoint" can be found within the list. The term "bigdata" in the list evaluates to False.
|