Python Booleans: Optimize Your Code With Truth Values

Python Booleans: Use Truth Values in Your Code

by Moshe Zadka Reading time estimate 39m intermediate python stdlib

The Python Boolean type is one of Python’s built-in data types. It’s used to represent the truth value of an expression. For example, the expression 1 <= 2 is True, while the expression 0 == 1 is False. Understanding how Python Boolean values behave is important to programming well in Python.

In this tutorial, you’ll learn how to:

  • Manipulate Boolean values with Boolean operators
  • Convert Booleans to other types
  • Convert other types to Python Booleans
  • Use Python Booleans to write efficient and readable Python code

The Python Boolean Type

The Python Boolean type has only two possible values:

  1. True
  2. False

No other value will have bool as its type. You can check the type of True and False with the built-in type():