Learn Python: Boolean Logic and Comparison Operators
Python uses booleans to represent truth values: True and False. Booleans emerge from comparison operators like == or <, as well as logical operators like and, or, and not. You can also test whether a value is considered “truthy” or “falsy,” which means Python treats it as True or False based on its content (an empty string is falsy, for instance). Booleans are central to making decisions in your code, and this lesson explains how to construct and evaluate them.
What will be covered
• Boolean data type: True and False.
• Comparison operators (==, !=, >, <, >=, <=)
and what they return.
• Logical operators (and, or, not) for combining or negating boolean expressions.
• Truthiness and falsiness in Python (e.g., empty string vs. non-empty string).
• Common scenarios where booleans are used (like flags or range checks).
Comparison Operators
A comparison operator compares two values and produces a boolean result. For instance, == checks if the two values are equal, while < checks if the left value is less than the right value. If the condition holds, Python returns True; otherwise, it returns False.
Each of these expressions evaluates to either True or False. You can print them or store them in a variable for later use.
Logical Operators (and, or, not)
The and operator returns True if both conditions are True. The or operator returns True if at least one condition is True. The not operator flips a boolean value: not True is False and not False is True.
x = 5
y = 10
print(x > 0 and y > 0)
print(x > 0 and y < 0)
print(x > 0 or y < 0)
print(not (x > 0))
In these examples, x > 0 and y > 0 is True if both conditions hold. x > 0 or y < 0 is True if at least the first part is True. The not operator inverts the result.
Truthiness and Falsiness
In Python, some non-boolean objects can be used where a boolean is expected. An empty string (""), the number 0, or an empty list ([]) are considered False. A non-empty string, a non-zero number, or a non-empty list are considered True. For now, just be aware that not all conditions have to explicitly say True or False. For example:
if "":
print("Empty string is True?")
else:
print("Empty string is Falsey!")
The empty string is considered Falsey, so the second branch of that conditional would run (once you learn about if statements).
Practical Uses of Booleans
Booleans often act as flags in your code. For instance, you might have a logged_in flag that checks if a user has properly authenticated. You could also check ranges to ensure a value is within certain bounds, or combine multiple comparisons to see if data meets all required conditions. These booleans then feed into branching logic (like if-else statements) to direct your program’s flow.
Practice Exercises
- Create a few comparison statements and predict their outcomes before running them. For example, check if 5 * 3 == 15, if 10 < 7, and if "abc" != "ABC". Print the results to see if your predictions match reality.
- Evaluate some compound boolean expressions. For instance, set x = 5, y = 10. Then print: (x > 0 and y > 0), (x > 0 and y < 0), (x > 0 or y < 0), and not (x > 0). Before running, guess which ones will be True or False, then compare with the printed results.
- Write a short snippet to check if a number n = 42 is between 1 and 100 (inclusive). Print "In range" if True, or "Out of range" if False. Make sure you only use concepts shown so far (boolean checks, no advanced control statements yet).
- Optional: assign text = "" (an empty string). Print bool(text) to see how Python interprets an empty string in a boolean context. Then assign text = "Hello" and print bool(text) again to compare.
Summary
You learned how to form boolean expressions with comparison operators like == and <, as well as logical operators and, or, and not. These produce True or False, which Python uses to make decisions. Understanding booleans prepares you for writing conditional logic (like if-else statements) and loops in upcoming lessons. You also saw how some non-boolean values can be treated as True or False, a concept known as truthiness. By mastering boolean expressions now, you gain a critical tool for creating dynamic, interactive Python programs.