a in b asks whether the text a appears anywhere inside b: "time" in "overtime" is True. It answers True or False, never where. Capitals count, so "Time" in "overtime" is False; to ignore case, compare lower-case copies. not in asks the opposite question. On a list, in asks about whole items: "Ann" in ["Ann Lee"] is False.
Try it
main.py
Output
Press Run and what your program prints shows up here.
Easy to get wrong
Thinks in gives the position of an item rather than True or False.
Thinks Python treats capital and lower-case letters as the same when comparing text.
Thinks "Ann" in ["Ann Lee"] is True because Ann is part of an item.
The Python in operator checks whether a value is inside a collection and gives back True or False: "Emma" in guests asks "is Emma one of the items in this list?". not in asks the opposite. It works on lists, tuples, sets, strings and dictionaries, and it is the usual way to write a membership test in an if, instead of a loop with a flag. What counts as "inside" depends on the type, and that difference catches people out.
How it works
On a list, in compares the value with each item, using ==, and the match has to be the whole item. On a string, it looks for a run of characters anywhere in the text. On a dictionary, it checks the keys.
guests = ["Emma", "Ann Lee"]
print("Ann" in guests)
print("Ann" in "Ann Lee")
print("ann" in "Ann Lee")
book = {"Emma": "Mill Lane"}
print("Emma" in book, "Mill Lane" in book)
print("Mill Lane" in book.values())
False
True
False
True False
True
"Ann" is not an item of guests: the item is "Ann Lee", and the two are not equal. Tested against the string "Ann Lee", though, "Ann" is part of the text, so the answer is True. Capital letters count, which is why "ann" is not found. For a dictionary, "Mill Lane" is a value, not a key, so in says False until you ask book.values() instead.
in answers whether. When you also need where, a list's .index() method gives the position:
guests = ["Emma", "Ann", "Lena"]
name = "Rosa"
if name not in guests:
guests.append(name)
print(guests)
print(guests.index("Lena"))
['Emma', 'Ann', 'Lena', 'Rosa']
2
.index() raises a ValueError if the item is missing, so it is common to check with in first. Testing a list goes through the items one at a time, which is fine for short lists. For thousands of lookups, a set or a dictionary answers in much faster.
Common mistakes
Expecting a list to match part of an item."Ann" in ["Ann Lee"] is False, because a list compares whole items. To find items that contain some text, loop over the list and test each string: if "Ann" in guest:.
Expecting a position.in gives True or False, never an index. Use guests.index(x) when you need to know where, after checking that x is there.
Expecting a dictionary to search its values.in on a dictionary checks only the keys. Write x in book.values() to search the values.
When to use it
if drink in ["tea", "coffee"]: is a tidy replacement for drink == "tea" or drink == "coffee"; see and, or, not for why drink == "tea" or "coffee" always counts as true. For speed with large collections, see membership speed. Looping over a list is the tool when you need each matching item, not only a yes or no.