In and is
in asks whether text contains a piece of text. is asks whether a value is None.
- Ask whether a piece of text appears inside other text
- See that capitals still count
- Ask whether a name is None
in asks whether the text on the left appears inside the text on the right: is there an @ in this email, does the ingredient list mention peanuts. "toy" in "toys" is True. "Toy" in "toys" is False, because capitals count. not in is the opposite question.
The piece you look for goes on the left. "SALE" in subject asks whether SALE appears in the subject. subject in "SALE" asks the reverse, which is almost never what you mean.
Run each example to see what it prints. Running examples doesn't count toward progress.
message = "Use WELCOME10 at checkout" print("WELCOME10" in message) print("welcome10" in message) print("SHIP" not in message)
is asks whether a value is None. coupon is None is True when there is no coupon. coupon is not None is True when a coupon was stored. Use is with None. Comparing ordinary text still uses ==.
coupon = None print(coupon is None) print(coupon is not None)
- in asks whether text contains a piece of text. The piece goes on the left. Capitals count.
- is None asks whether a name holds None. is not None asks the opposite.
- == still compares ordinary values. is is for None. Next: which operation happens first.