Chapters
Python114 chapters

BasicsChapter 12 of 114

Booleans

True, False, and the rule that decides whether any value counts as either.

Two values

True and False are the whole type. Both are capitalised, and both are keywords rather than strings:

Python
is_open = True
is_locked = False
print(is_open, is_locked)
print(type(is_open))

Output

True False
<class 'bool'>

Comparisons produce booleans

Any comparison evaluates to one of the two:

Python
print(10 > 3)
print(10 == 3)
print("a" < "b")
print(3 in [1, 2, 3])

Output

True
False
True
True

Truthiness

Every value can stand in for a condition. The rule is short: empty and zero are false, everything else is true.

Falsy values, in full:

  • False and None
  • zero of any numeric type: 0, 0.0
  • empty sequences and collections: "", [], (), {}, set()
Python
print(bool(0), bool(0.0), bool(None))
print(bool(""), bool([]), bool({}))
print(bool(1), bool(-1), bool("no"), bool([0]))

Output

False False False
False False False
True True True True

Note bool([0]) is True: the list holds one item, so it is not empty. What is in it does not matter.

Using truthiness

This is why Python code checks emptiness directly rather than comparing lengths:

Python
items = []

if items:
    print("we have items")
else:
    print("nothing here")

Output

nothing here

if items: is preferred over if len(items) > 0: — shorter, and it works for any container.

and, or, not

Python
print(True and False)
print(True or False)
print(not True)

Output

False
True
False

These do not return True or False — they return one of the operands, and stop as soon as the answer is settled:

Python
print(0 or "fallback")
print("given" or "fallback")
print(1 and "second")

Output

fallback
given
second

That short-circuit is used deliberately for defaults, and it is also why name or "Anonymous" is such a common line.

Booleans are integers

bool is a subclass of int, with True worth 1 and False worth 0:

Python
print(True + True)
print(sum([True, False, True]))

Output

2
2

Summing a list of comparisons to count matches is a genuinely useful trick:

Python
scores = [45, 80, 62, 95]
print(sum(score > 60 for score in scores), "passed")

Output

3 passed

any and all

Python
print(any([False, False, True]))
print(all([True, True, False]))
print(all([]))

Output

True
False
True

all([]) is True: there is no item that fails, so the claim holds. It catches people out, and it is the mathematically consistent answer.

Test yourself

3 questions

Which of these is truthy?

Show the answer

[0] — The list holds one item, so it is not empty. What is inside it does not matter.

What does 0 or "fallback" give?

Show the answer

"fallback" — or returns one of the operands rather than a boolean, which is why it is used for defaults.

What is all([])?

Show the answer

True — No item fails the test, so the claim holds. It surprises people and it is the consistent answer.

Next chapter

Operators

Arithmetic, comparison, logic, membership and identity, plus what binds tightest.