In this lesson, you will learn how Python represents logical truth using the Boolean constants True and False to form the foundation of your code’s decision-making.
The Foundation of Logic
At the heart of every logical decision in Python are two special values: True and False. These are known as Booleans (named after the mathematician George Boole). In programming, we use these values to answer questions about our data. For example, is a number greater than another? Is a specific string present in a list? The result of these questions is always one of those two Boolean constants.
Unlike strings or numbers, True and False are keywords in Python, meaning they are built-in and do not need quotes. Note that they must always start with a capital letter.
Seeing Truthiness in Action
You can create Boolean values by asking Python to compare two pieces of data. Python evaluates these expressions and provides you with the corresponding Boolean result:
# Comparing numeric values
is_greater = 10 > 5
print(is_greater) # Output: True
# Checking for equality
is_equal = 5 == 2
print(is_equal) # Output: False
In the code above, 10 > 5 is a logical expression that Python evaluates. Since 10 is indeed larger than 5, Python returns the Boolean value True. Conversely, since 5 does not equal 2, Python returns False.
Why This Matters
Everything in Python—from simple if statements that control which code runs, to complex loops that process data—relies on these evaluations. By representing "yes" as True and "no" as False, Python allows your program to react dynamically to input, calculations, or conditions you define. Think of these Boolean values as the "on" and "off" switches for your logic flow.
Key Takeaway: Boolean values True and False act as the fundamental answers to logical questions, allowing your program to distinguish between valid and invalid conditions.