Blog Post #21: The Core of Logic: Understanding True, False, and Boolean Expressions

Welcome to Part 2 of our Python series: Logic and Control Flow! In Part 1, we built programs that were like a set of instructions read from top to bottom. They were powerful, but they followed the same path every time. Now, we’re going to learn how to make our programs “think”—how to make decisions, repeat actions, and follow different paths based on the conditions they encounter.

To do this, we must first master the foundation of all program logic: boolean values. We briefly met them in Post #11, but in this post, we’ll take a deeper look at True, False, and the critical concept of a Boolean Expression.

A Refresher on Booleans: The Two-Value System

As we learned in Post #11, the boolean (bool) data type is incredibly simple. It can only ever have one of two possible values: True or False.

Remember, the capitalization is crucial. They are Python keywords, not strings.

These two values are the digital equivalent of a light switch being ON (True) or OFF (False). Every decision your program will ever make, no matter how complex, ultimately boils down to one of these two outcomes.

Introducing Boolean Expressions

While True and False are the answers, the questions we ask to get these answers are called Boolean expressions.

A Boolean expression is any piece of code that evaluates to either True or False. It’s a statement that Python can judge as being factually correct or incorrect within the context of the program.

The simplest Boolean expressions are the values themselves or variables that hold them.

is_raining = True
has_umbrella = False

# In this code, 'is_raining' is a boolean expression. Its value is True.
# 'has_umbrella' is also a boolean expression. Its value is False.

However, the most common way to generate boolean values is by comparing things, which we’ll dive into in our next post. A statement like 5 > 3 is a boolean expression because Python can evaluate it and determine that its value is True.

The Foundation of Decisions

So why is this so important? Boolean expressions are the heart of control flow structures like if statements. An if statement checks if a boolean expression is True, and if it is, it runs a specific block of code. This is how we make our programs follow different paths.

Let’s look at a quick sneak peek of how this works in a mini game scenario.

# Let's set a condition
player_has_red_key = True

print("You are standing in front of a locked red door.")

# This is a preview of an 'if' statement.
# It evaluates the boolean expression 'player_has_red_key'.
if player_has_red_key:
    # This code block only runs because the expression is True.
    print("You use the red key. The door swings open!")

The if statement on line 7 checks the value of the player_has_red_key variable. Since it is True, the indented print() statement on line 9 is executed.

Try changing the first line to player_has_red_key = False and run the script again. You’ll see that the final message, “You use the red key…”, does not appear. This is decision-making in action!

What’s Next?

You now understand that all program logic is built on the simple foundation of True and False values, and that Boolean expressions are the “questions” we ask our code to produce these values.

We’ve seen that a variable can be a boolean expression, but how do we create these expressions on the fly by comparing different values? In Post #22, we will dive into the essential tools for asking questions in Python: the comparison operators.

Author

Debjeet Bhowmik

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

Leave a Comment