Boolean Conditions
Understand True and False, comparison expressions, and how Python evaluates conditions using practical examples and exercises.
In this lesson, you will learn
- ✓Explain the purpose of Boolean values and identify Python's Boolean literals.
- ✓Use comparison operators to determine relationships between values.
- ✓Trace Boolean expressions and predict their exact output.
- ✓Explain how Python evaluates truthy and falsy values in conditions.
- ✓Write and test simple Boolean conditions using realistic examples.
1. Understanding Boolean Values
A Boolean value represents one of two possibilities: True or False. Python uses the built-in bool type for these values. Boolean values are useful whenever a program needs to represent whether something is true, false, satisfied, or not satisfied.
For example, a program might check whether a student has passed an exam, whether a password is valid, or whether a temperature exceeds a limit. Each check can produce a Boolean result.
Python's Boolean literals are written as True and False, with capital first letters. The lowercase names true and false are not Python Boolean literals.
A comparison expression can produce a Boolean value. You can store that value in a variable, print it, or use it in a condition.
Store and display a Boolean value
The variable is_learning refers to the Boolean value True. The variable has_finished refers to the Boolean value False. The type() function reports that is_learning contains a value of type bool. Each print() call displays its result on a separate line.
Note
2. Comparison Operators
Comparison operators compare two values and produce a Boolean result. They let a program check equality, inequality, and ordering relationships.
The equality operator is ==, while the assignment operator is =. Use = to assign a value to a variable and == to test whether two values are equal.
When comparing numeric values, Python evaluates the relationship and returns True if it holds or False if it does not. The same comparison operators can also work with other compatible types, such as strings, although ordering depends on the type being compared.
Compare numeric values
temperature > 20 is True because 24 is greater than 20. temperature == 24 is True because both values are equal. temperature < 10 is False because 24 is not less than 10. temperature != 24 is False because the two values are equal.
Note
3. Reading and Tracing Conditions
To understand a Boolean expression, evaluate the values involved before deciding whether the expression is True or False. Reading a condition carefully is more reliable than guessing its result.
When a variable is involved, first identify its current value. Then apply the comparison operator and determine the resulting Boolean value.
A dry run records the important steps in execution order. It helps you connect each expression with the value Python evaluates.
Trace a comparison step by step
First, score is assigned the integer value 75. Next, score >= 60 compares 75 with 60 and evaluates to True. The resulting Boolean value is assigned to passed. The first print() displays 75, the second displays True, and the third confirms that score equals 75.
Caution
4. Combining Comparisons with and, or, and not
Python provides the logical operators and, or, and not to combine or reverse conditions. They are useful when a decision depends on more than one requirement.
The and operator requires both operands to be truthy for its result to be truthy. The or operator produces a truthy result when at least one operand is truthy. The not operator reverses the truth value of its operand.
When both operands are Boolean values, these operators make it easy to express conditions such as whether a score is within a range or whether a user has met at least one requirement.
Combine two conditions
The first expression is True because age >= 18 and has_id are both True. The second expression is True because has_id is True, even though age < 18 is False. The third expression is False because not reverses the value True.
Note
5. Truthy and Falsy Values
Python conditions do not require the expression to be exactly True or False. Python can evaluate other values in a Boolean context, such as an if condition.
A truthy value behaves like True in a Boolean context, while a falsy value behaves like False. Common falsy values include False, None, numeric zero, and empty collections or strings, such as 0, 0.0, '', [], (), and {}. Most other values are truthy.
The bool() function lets you explicitly check how Python interprets a value in a Boolean context. This is useful for learning and for checking values, but it does not mean that the original value changes into a Boolean.
Check truthiness with bool()
The integer zero is falsy, while the nonzero integer 7 is truthy. An empty string is falsy, while a non-empty string is truthy. An empty list is falsy, while a list containing elements is truthy. bool() returns a Boolean value representing the truthiness of each supplied value.
Caution
6. Chained Comparisons and Real-World Conditions
Python supports chained comparisons, which let you compare one value against a lower and an upper bound in a readable expression. For example, 18 <= age <= 60 checks both age >= 18 and age <= 60.
A chained comparison is often clearer than repeating a variable in separate comparisons joined by and. Python evaluates the comparisons as a chain, and the overall result is Boolean.
Boolean conditions appear in everyday programs. They can determine whether a score meets a passing threshold, whether a number falls within a permitted range, or whether a value is present before it is processed.
Use a chained comparison
When age is 22, it is at least 18 and at most 60, so the chained comparison is True. When age is 15, the lower-bound comparison fails, so the chained comparison is False.
Note
Common pitfalls to avoid
Real debugging lessons and exact syntax contrasts to avoid runtime bugs
#1 Using = when comparing two values.
Why it happens: A single = is assignment syntax; comparisons in conditions generally use == or another comparison operator.
Key takeaway: Use = for assignment and == for equality comparisons. For example, score == 60 checks whether score equals 60.
#2 Writing true or false in lowercase.
Why it happens: Python Boolean literals are capitalized as True and False; lowercase true and false are treated as undefined names unless assigned.
Key takeaway: Write Python's Boolean literals as True and False. Lowercase true and false are not the built-in Boolean literals.
#3 Assuming a comparison always produces True.
Why it happens: A comparison evaluates the actual values and can return False when the condition is not satisfied.
Key takeaway: Evaluate the actual values. For example, 4 >= 8 is False because four is less than eight.
#4 Using and when either condition should be sufficient.
Why it happens: and requires both conditions to be true; use or when either condition should independently satisfy the requirement.
Key takeaway: Use and when both conditions must hold and or when at least one condition must hold.
#5 Assuming only Boolean values can be conditions.
Why it happens: Python conditions accept truthy and falsy values as well as explicit Boolean expressions.
Key takeaway: Python also evaluates truthiness. Values such as 0 and an empty string are falsy, while nonzero numbers and non-empty strings are truthy.
#6 Assuming the string "0" behaves like the integer 0 in a Boolean context.
Why it happens: The string "0" is non-empty and therefore truthy, while the integer 0 is falsy.
Key takeaway: The integer 0 is falsy, but the string "0" is truthy because it is not empty.
#7 Ignoring boundary values when checking a range.
Why it happens: Inclusive and exclusive boundaries differ; test values at the minimum and maximum, not only values inside the range.
Key takeaway: Test values at the lower and upper limits, as well as values just outside them, to confirm that comparison operators match the requirement.
Ready for hands-on challenges?
Test your skills with shuffled coding challenges in the Practice Arena.
Technical and placement interview questions
Frequently tested interview questions with detailed, full answers (click to expand)
Q1:What is a Boolean value in Python?
Q2:What is the difference between = and ==?
Q3:What do Python's comparison operators do?
Q4:What is the difference between and and or?
Q5:What does not do?
Q6:What are truthy and falsy values?
Q7:What is a chained comparison?
Q8:How would you debug an incorrect Boolean condition?
Key takeaways
- Python represents Boolean values using True and False, and their type is bool.
- Comparison operators produce Boolean results; use == for equality and = for assignment.
- Use and, or, and not to combine or reverse conditions, remembering their evaluation rules.
- Python evaluates truthiness in conditions. Zero and empty collections are falsy, while non-empty strings such as "0" are truthy.
- Chained comparisons provide a readable way to check whether a value falls within a range.
- Trace expressions and test boundary cases instead of guessing their results.
Related Topics & Next Steps
4 Recommended LessonsStrengthen your Python foundation with these complementary core lessons: