loop
Operations on lists

Module 3 · Operations on lists · Lesson 4 of 5

The in and not in operators

Very often you don't care where a value sits in a list — only whether it's there at all. Python has a pair of operators built exactly for that question: in and not in. Each one looks through the list and answers with a plain True or False.

6 in nums
38169

Under the hood it's exactly the scan you just watched: Python compares the value to each element in turn and stops the moment it finds a match. You never have to write that loop yourself — 6 in nums says it all.

Run it yourselfruns in your browser · Python
main.py

Because the result is a Boolean, it slots straight into an if — “if this value is in the list, do something.” not in is simply its opposite, and reads just as naturally. (As a bonus, both work on strings, where they test for a substring.)