runtime
14 / 250
lesson 14 Operators & Expressions · beginner

In and is

in asks whether text contains a piece of text. is asks whether a value is None.

You'll be able to
  • Ask whether a piece of text appears inside other text
  • See that capitals still count
  • Ask whether a name is None
The idea

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.

example-1.py · A code inside a message
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 ==.

example-2.py · Is there a coupon?
coupon = None
print(coupon is None)
print(coupon is not None)
Review
  • 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.
Practise this

Loading your progress…

To master this lesson, solve both challenges without hints or the answer, and pass the concept check.

In and is — Operators & Expressions · Runtime