Boolean
In computer science, a Boolean (or bool) is a fundamental data type that can have only one of two values: true or false. These values serve as the foundation of logical operations and control flow in programming, representing binary states like yes/no, on/off, or 1/0.
Booleans are named after George Boole, a 19th-century mathematician who developed Boolean algebra, a branch of mathematics that operates on true/false values.
In Python, the Boolean type is implemented as a subclass of the int
class, where True
has a value of 1 and False
has a value of 0, though they are written and displayed as True
and False
respectively.
The Boolean data type is essential for:
- Making decisions in conditional statements (
if
/else
) - Controlling loop execution (
while
/for
) - Evaluating logical expressions (
and
/or
/not
) - Storing binary state information (flags)
Python treats certain values as “truthy” or “falsy” when used in a Boolean context. Any empty or zero value is considered False
, while most other values are considered True
. This behavior allows for convenient shorthand in conditional expressions while maintaining logical clarity in code.
Related Resources
Tutorial
Python Booleans: Use Truth Values in Your Code
In this tutorial, you'll learn about the built-in Python Boolean data type, which is used to represent the truth value of an expression. You'll see how to use Booleans to compare values, check for identity and membership, and control the flow of your programs with conditionals.
For additional information on related topics, take a look at the following resources:
By Dan Bader • Updated Jan. 10, 2025