Python Tutorial · Python Basics · Lesson 11 of 42

Python Booleans

True, False, comparisons and which values count as truthy or falsy.

True and False

Comparisons produce booleans:

Example 1Try it yourself
print(10 > 9)
print(10 == 9)
print(10 != 9)
print("a" < "b")

Output

True
False
True
True

Truthy and falsy values

In conditions, every value counts as true or false. Falsy values are: False, None, 0, 0.0, empty strings "", and empty collections [], (), {}, set(). Everything else is truthy.

Example 2Try it yourself
for value in [0, 1, "", "hi", [], [0], None, {}]:
    print(repr(value), bool(value))

Output

0 False
1 True
'' False
'hi' True
[] False
[0] True
None False
{} False

This lets you write if items: instead of if len(items) > 0:.

bool is a kind of int

True and False behave like 1 and 0 in arithmetic — useful for counting:

Example 3Try it yourself
answers = [True, False, True, True]
print(sum(answers), "correct")
print(True + True)

Output

3 correct
2

Test yourself

Which value is truthy?

  1. 0
  2. ""
  3. []
  4. [0]
Show answer

Answer: [0]

A list containing one element is non-empty, so it's truthy — even if that element is 0.