8.4. The in and not in operators

The in operator tests if one string is a substring of another:

Note that a string is a substring of itself, and the empty string is a substring of any other string. (Also note that computer scientists like to think about these edge cases quite carefully!)

The not in operator returns the logical opposite result of in.

We can also use the in and not in operators on lists!

..activecode:: ac4_4_4

print(“a” in [“a”, “b”, “c”, “d”]) print(9 in [3, 2, 9, 10, 9.0]) print(‘wow’ not in [‘gee wiz’, ‘gosh golly’, ‘wow’, ‘amazing’])

However, remember how you were able to check to see if an “a” was in “apple”? Let’s try that again to see if there’s an “a” somewhere in the following list.

Clearly, we can tell that a is in the word apple, and absolutely, and application. For some reason though, the Python interpreter returns False. Why is that? When we use the in and not in operators on lists, Python checks to see if the item on the left side of the expression is equivalent to an element in the item on the right side of the expression. In this case, Python is checking whether or not an element of the list is the string “a” - nothing more or less than that.

Next Section - 8.5. Precedence of Operators