Python Booleans

Python Booleans

Booleans represent one of two values: True or False.

In programming you often need to know if an expression is True or False. You can evaluate any expression in Python, and get one of two answers, True or False.

Evaluating Expressions

print(10 > 9) // True
print(10 == 9) // False
print(10 < 9)  // False

Evaluate Values and Variables

The bool() function allows you to evaluate any value, and give you True or False in return.

The bool() function

print(bool("Hello")) // True
print(bool(15))      // True

Most Values are True

Almost any value is evaluated to True if it has some sort of content.

Some Values are False

In fact, there are not many values that evaluate to False, except empty values, such as (), [], {}, "", the number 0, and the value None. And of course the value False evaluates to False.

Falsy Values

print(bool(False))
print(bool(None))
print(bool(0))
print(bool(""))
print(bool(()))
print(bool([]))
print(bool({}))

Exercise

?

Which of the following evaluates to `False` in Python?