So far in our journey, we have learned how to work with numbers (int
, float
) in Post #9 and text (str
) in Post #10. These data types are essential for storing information. But to create intelligent programs that can make decisions, we need a way to represent the concepts of ‘yes’ and ‘no’, ‘on’ and ‘off’, or ‘true’ and ‘false’.
This is where the boolean data type comes in. In this post, we will explore this simple but incredibly powerful data type that forms the bedrock of all logic in Python.
What is a Boolean?
A boolean is the simplest possible data type. It can only hold one of two possible values: True
or False
. That’s it. There are no other options.
Think of it as a light switch that can only be in the “on” or “off” position, or the answer to a direct yes-or-no question. In Python, the name for this data type is bool
.
Crucial Syntax: It is critical to remember that the boolean values in Python are keywords and must be written with a capital first letter: True
and False
. The lowercase versions true
and false
are not valid and will cause an error in your code.
Creating Boolean Variables
You can create a boolean variable just like any other variable, by assigning one of the two boolean values to it. These variables often act as “flags” to keep track of a state within your program.
is_game_over = False
is_logged_in = True
has_admin_permission = True
print(is_game_over)
print(is_logged_in)
When you run this code, the output will be:
False
True
These variables are incredibly useful. For example, your program could check if is_logged_in
is True
before allowing a user to see a certain page or access a feature.
Where Do Booleans Come From?
While you can assign True
or False
directly, booleans are most often the result of a comparison. You can ask Python a question, and it will answer you with a boolean.
We will cover comparison operators in much more detail later, but let’s take a quick sneak peek. You can ask Python questions using operators like >
(greater than) or ==
(is equal to).
Let’s ask Python a couple of questions:
# Ask Python: "Is 10 greater than 5?"
result = 10 > 5
print(result)
# Ask Python: "Is 2 equal to 3?"
are_they_equal = 2 == 3
print(are_they_equal)
The output of this code will be:
True
False
This is the essence of logic in programming. You ask a question (make a comparison), Python gives you a True
or False
answer, and then your program can use that answer to decide what to do next.
What’s Next?
You’ve now learned about the boolean (bool
) data type and its two values, True
and False
. While simple, they are the fundamental units of logic that allow our programs to make decisions and follow different paths.
Now that we have a basic understanding of numbers, strings, and booleans, it’s time to start making them interact. In Post #12, we will learn how to perform calculations in Python by mastering the basic arithmetic operators like addition, subtraction, multiplication, and division.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com