Boolean Logic
Understand logical True and False states, verify comparisons, and evaluate truthy vs falsy values using bool().
1. What are Booleans?
In programming, you often need to know if a statement is true or false. Python has a data type called **Boolean** (bool) that can only hold one of two values: True or False.
2. Booleans from Comparisons
When you compare two values, Python evaluates the expression and returns a boolean result:
print(10 > 9) # Output: True
print(10 == 9) # Output: False
3. Truthy vs. Falsy Values
Almost any value in Python can be evaluated as a Boolean using the bool() constructor. Python categorizes values as **Truthy** (evaluates to True) or **Falsy** (evaluates to False):
- Falsy Values: Empty strings (
""), numeric zero (0), decimal zero (0.0), and the special valueNone. - Truthy Values: Any string containing characters (even a space
" "), any non-zero number, and non-empty lists/tuples.
💻 Python Code Examples
Example 1: Boolean Literals
is_active = True
is_finished = False
print(is_active, is_finished)
Description: Declares variables using boolean literals (capitalization required).
Example 2: Booleans from comparisons
x = 10
y = 20
print(x < y) # True
print(x == y) # False
Description: Shows how comparisons return Boolean results.
Example 3: Evaluating Truthy Numbers
print(bool(100))
print(bool(-5))
# Any non-zero number is True
Description: Shows that positive and negative numbers evaluate to True.
Example 4: Evaluating Falsy Zeroes
print(bool(0))
print(bool(0.0))
# Numeric zero evaluates to False
Description: Shows that both integer and float representations of zero are falsy.
Example 5: Truthy vs. Falsy Strings
print(bool("Hello")) # True
print(bool("")) # False (empty)
print(bool(" ")) # True (contains space)
Description: Only empty strings evaluate to False; strings containing spaces are truthy.
Example 6: Comparing Booleans Directly
a = True
b = False
print(a != b) # Output: True
Description: Compares boolean variables using comparison operators.
🧠 Active Retrieval Quizzes
Answer the following 10 multiple-choice questions to test your storage strength.
Question 1: What are the two valid values for a Boolean variable in Python?
Question 2: What is the output of print(10 == 9)?
Question 3: Which function checks the Boolean value of an object?
Question 4: Which of these values is Falsy and evaluates to False?
Question 5: What does the expression bool(' ') (string containing a single space) return?
Question 6: What is the boolean evaluation of an empty string: bool('')?
Question 7: What does bool(0.0) return?
Question 8: What is the output of print(5 < 10)?
Question 9: Is the string 'False' truthy or falsy: bool('False')?
Question 10: Which of these is a truthy value?
🖥️ Output Prediction Challenges
Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.
Predict the output of the code snippet below. Type the exact output in the console.
print(10 > 5)
Predict the output of the code snippet below. Type the exact output in the console.
print(bool(0))
Predict the output of the code snippet below. Type the exact output in the console.
print(bool("Python"))
Predict the output of the code snippet below. Type the exact output in the console.
print(bool(""))
Predict the output of the code snippet below. Type the exact output in the console.
print(5 == 5.0)
🎮 Interactive Code Playground
Type your own Python code below and click "Run Code" to execute it instantly in your browser!
Console ready.
Stuck on any concept, or want to explore an edge case? Don't hesitate to ask follow-up questions to your instructor! Active questioning helps build storage strength.
📋 Progress Status
📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.