Hone

Lessons · Python · in (is it inside?)

Asking whether something is there

in asks a yes or no question and answers True or False.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Is this email already registered. Does this order contain a discount code. Is this word in the banned list. Checking membership is one of the most common things code does.

How to think about it

Ask: do I need to know IF it is there, or WHERE it is? in answers the first. index() answers the second and raises when it is missing.

Worked example

names = ["ada", "grace"]
The list we are asking about.
print("ada" in names)
True. It looks for a whole item, not part of one.
print("ad" in names)
False. "ad" is not an item, even though it starts one.
print("ad" in "ada")
True. On TEXT, in looks for a run of characters anywhere inside, which is a different question.

Your turn

Check whether 5 is in a list of numbers.

nums = [1, 5, 9]
print(5  nums)

The trap

in means two different things depending on what you ask. Whole items for a list, any run of characters for text.

Practise in (is it inside?) on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.