🎓 A1Learner
🔍

Python Booleans

A boolean is a value that is True or False. A comparison makes a boolean.

Two values only

A bool variable holds one of two values.

is_open = True
is_closed = False
print(is_open)
print(type(is_open))

Comparisons make booleans

Here == asks if two values are equal. The signs > and < work like in maths.

print(5 == 5)
print(5 > 8)
print(3 < 7)

Judging numbers

Python calls a value truthy when bool() gives True. It calls a value falsy when bool() gives False.

print(bool(1))
print(bool(0))
print(bool(-3))

Empty means False

An empty string has no characters. An empty list has no items.

print(bool("hi"))
print(bool(""))
print(bool([1, 2]))
print(bool([]))

Example

print(bool(10))
print(bool(0))
print(bool("Ada"))
print(bool(""))
print(bool([5]))
print(bool([]))

Key points

  • Bool: the type that holds True or False
  • Truthy: a value that bool() turns into True
  • Falsy: a value that bool() turns into False
  • Use bool() to ask how Python judges a value
  • Empty values are falsy, like an empty string, an empty list, and 0
  • A comparison makes a boolean, like 5 == 5

In this example, each line asks bool() to judge one value. The number 10 is truthy, so it prints True. The number 0 is falsy, so it prints False. The text "Ada" has characters, so it prints True. The empty text prints False. The list [5] has an item, so it prints True. The empty list prints False.

Note

In Python, True and False start with a capital letter.

The word true in small letters is not a value.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What does bool() give for an empty string?