Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 2 · Boolean Values and Logical Expressions

Boolean Values and Logical Expressions

On this pageBoolean Values:True and FalseComparison OperatorsLogical OperatorsUsing Booleans in ConditionsMini Project — Temperature Checker

A Boolean has one of two values, True or False. Comparisons and logical operators let a program combine answers to questions such as “is the age at least 18?”

Boolean Values:True and False

Python uses the keywords True and False (with capital letters) to represent Boolean values. They can appear directly or result from comparisons and logical operations.

print(True)
print(False)
Listing 73 — Boolean Values: True and False

Output

True
False

Booleans may seem simple, but they are powerful. They act as the foundation for conditions in if statements and loops that drive program logic.

Comparison Operators

A Boolean value often comes from comparing two expressions. Python provides several comparison operators that return either True or False.

Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 7 > 10 False
< Less than 2 < 5 True
>= Greater than or equal to 10 >= 10 True
<= Less than or equal to 4 <= 3 False
Table 2 — Comparison Operators

Try running a few comparisons:

print(10 > 3)
print(8 == 5)
print(7 != 2)
Listing 74 — Comparison Operators

Output

True
False
True

Each expression evaluates to a Boolean value, allowing programs to respond differently depending on whether a condition is true or false.

Logical Operators

Sometimes you need to combine multiple conditions. For the Boolean values used here, and, or and not give the results in the table. More generally, and and or return one of their operands, while not always returns a Boolean:

Operator Meaning Example Result
and True if both conditions are True (5 > 3) and (8 > 6) True
or True if at least one condition is True (5 > 10) or (8 > 6) True
not Inverts the Boolean value not (5 > 3) False
Table 3 — Logical Operators

Example:

age = 20
has_ticket = True

print(age >= 18 and has_ticket)
print(age < 18 or has_ticket)
print(not has_ticket)
Listing 75 — Logical Operators

Output

True
True
False

In the first line, both conditions are true, so the result is True.

In the second, only one is true—but because we used or, the overall result is still True.

Finally,not reverses the truth value.

Using Booleans in Conditions

The following is a preview of conditional statements, developed in Control Flow. An if line tests a condition, and an else block runs when it is false. Indented statements form each block. They are most often used in if statements, where the program checks whether a condition is true before performing an action.

is_raining = False

if is_raining:
    print("Take an umbrella!")
else:
    print("Enjoy the sunshine!")
Listing 76 — Using Booleans in Conditions

Here, the variable is_raining holds a Boolean value.

Python checks it and chooses which message to display based on the result.

Mini Project — Temperature Checker

Write a program that helps decide what to wear based on temperature.

Ask the user for today’s temperature and then use Boolean logic to print a suggestion.

Instructions:

  • Convert the input to a number.
  • Use conditions to check if it is cold (below 15°C), warm (15–25°C), or hot (above 25°C).
  • Print a short message for each case.

Example Run:

Example Run 1:
Enter the temperature (°C): 12
It’s cold today. Wear a jacket!

Example Run 2:
Enter the temperature (°C): 20
The weather is warm and pleasant.

Example Run 3:
Enter the temperature (°C): 30
It’s hot today. Stay hydrated and wear light clothes!
Listing 77 — Mini Project — Temperature Checker

This project introduces the idea of Boolean-driven decisions and prepares you for the control flow chapter that follows.