runtime
19 / 250
lesson 19 Conditional Logic · beginner

What counts as true

A test can be the value itself. Empty text, 0, and None count as false.

You'll be able to
  • Use a name by itself as the test
  • Treat empty text, 0, and None as false
  • Treat other text and other numbers as true
The idea

A comparison is not the only test. if name: asks whether the value itself counts as true. It is the short, usual way to ask "is there anything here?": a nickname that may be blank, a coupon that may be missing, a cart that may be empty.

Empty text "", the number 0, and None count as false. Any other text, and any other number, counts as true. True and False still mean what they did before.

Run each example to see what it prints. Running examples doesn't count toward progress.

example-1.py · Empty text is false
code = ""
if code:
    print("Has a code")
else:
    print("No code")
example-2.py · None and zero
coupon = None
qty = 0
if coupon:
    print("Coupon")
else:
    print("No coupon")
if qty:
    print("Some")
else:
    print("Zero")

Mistake to avoid: text is true whenever it isn't empty, whatever it says. "0", "False" and " " (one space) all count as true. Only "" is false. And if qty: treats 0 as "nothing", which is right for a count but wrong if 0 is a real value you care about, like a temperature of 0.

Review
  • if name: uses the value itself as the test.
  • Empty text, 0, and None count as false. Other text and other numbers count as true.
  • "0" and "False" are text, so they count as true. Next: choosing a value on one line, and match.
Practise this

Loading your progress…

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

What counts as true — Conditional Logic · Runtime