Combining Boolean Expressions with and, or, and not
A boolean expression evaluates to True or False, which belong to the bool data type.
From One Question to Several
A boolean expression is an expression that evaluates to True or False. These two values belong to Python's bool data type. A single comparison asks one question, such as whether one value is greater than another. Logical operators let you combine such questions into a larger boolean expression.
The expression above contains two comparisons joined by and. To understand the final result, evaluate each comparison first, then apply the logical operator that joins them.
How Logical Operators Combine Results
The operator and produces True only when both boolean expressions on its sides are True. The operator or produces True when at least one of its two expressions is True. The operator not reverses a boolean result: not True becomes False, and not False becomes True. Each smaller expression should be understood as producing a boolean value before you determine the result of the whole expression.
| Operator | Result rule | Example result |
|---|---|---|
| and | True only when both inputs are True | True and False produces False |
| or | True when at least one input is True | True or False produces True |
| not | Reverses one boolean result | not False produces True |
Rules for combining boolean results
Tracing a Combined Expression
What do you think happens?
What value does this expression produce: 8 > 3 and 8 < 10?
Reveal answer
Answer: True
The comparison 8 > 3 is True, and the comparison 8 < 10 is also True. Because and requires both sides to be True, the combined expression produces True.
Checking a Range
Determine the result of 8 > 3 and 8 < 10.
Evaluate the left comparison: 8 is greater than 3, so 8 > 3 produces True.
Evaluate the right comparison: 8 is less than 10, so 8 < 10 produces True.
Apply and: Both inputs to and are True, so the combined expression produces True.
True
TrueBuilding the Comparisons
Every comparison expression has a left operand, a comparison operator, and a right operand. Python examines the two operands, applies the operator's rule, and produces True or False. These comparison results can then be combined with and, or, or not.
| Operator | Question it asks | Example |
|---|---|---|
| == | Do the two operands have equal values? | 5 == 5 |
| != | Do the two operands have different values? | 5 != 3 |
| > | Is the left operand strictly greater? | 5 > 3 |
| < | Is the left operand strictly smaller? | 3 < 5 |
| >= | Is the left operand greater or equal? | 5 >= 5 |
| <= | Is the left operand smaller or equal? | 5 <= 5 |
| is | Do the operands refer to the exact same object? | first is second |
| is not | Do the operands refer to different objects? | first is not second |
Python's eight comparison operators
False
True
False
TrueAssignment and Comparison
The single equals sign, =, assigns a value. The double equals sign, ==, compares values and produces a boolean result. These operators are not interchangeable. Writing = when you intend a comparison is a common and critical error.
Equality and Identity
The operators == and is both compare two operands, but they ask different questions. The == operator compares the values of the operands. The is operator checks whether the operands refer to the exact same object in memory. Two objects can contain equal values without being the same object.
True
FalseIn this example, first and second contain equal values, so == produces True. They are nevertheless separate objects, so is produces False. For simple values such as numbers and strings, the distinction often does not matter, but for more complex objects it can produce different results.
Mistakes to Avoid
Using = when you mean ==.
The single equals sign assigns a value; it does not compare two values.
Fix:
Use score == 5 when you want a boolean comparison.Treating > and >= as interchangeable.
The > operator is exclusive and does not return True when the operands are equal.
Fix:
Use >= when equality should also count.Using is to test whether two values are equal.
The is operator tests whether two operands refer to the same object, not merely whether their values are equal.
Fix:
Use == for value equality.Trying to decide the combined result before evaluating its component comparisons.
A combined expression depends on the boolean result of each comparison.
Fix:
Evaluate each comparison first, then apply and, or, or not.
Practice Before Execution
Predict the result of each expression before evaluating it: 1. 4 < 9 and 4 >= 4 2. 4 > 9 or 4 == 4 3. not (4 < 9) 4. 4 == 4 5. 4 is 4. For each answer, identify the comparison results first and then explain how the logical operator affects them.
Hints
- The first expression uses and, so both comparisons must be True.
- The second expression uses or, so one True comparison is enough.
- The not operator reverses the result of the expression in parentheses.
- Compare values with == and object identity with is.
One Practice Solution
Predict the result of 4 > 9 or 4 == 4.
Evaluate the first comparison: 4 is not greater than 9, so 4 > 9 produces False.
Evaluate the second comparison: 4 has the same value as 4, so 4 == 4 produces True.
Apply or: At least one input to or is True, so the combined expression produces True.
True
Reliable Evaluation Routine
- Separate the expression into its individual comparisons.
- Identify the left operand, operator, and right operand in each comparison.
- Check whether each comparison produces True or False.
- Apply and, or, or not to those boolean results.
- Check whether you used = for assignment or == for value comparison.
- If you used is or is not, confirm that you are asking about object identity rather than value equality.
- Boolean expressions produce True or False. Comparison expressions have a left operand, an operator, and a right operand. Use and when both conditions must be True, or when at least one condition may be True, and not to reverse a boolean result. Remember that = assigns, while == compares values. Python's eight comparison operators are ==, !=, >, <, >=, <=, is, and is not. Finally, == asks whether values are equal, while is asks whether two operands refer to the same object.
Key Takeaways
- A boolean expression evaluates to one of two bool values: True or False.
- Use and, or, and not to combine or reverse boolean results.
- Use = for assignment and == for comparing values.
- Python has eight comparison operators: ==, !=, >, <, >=, <=, is, and is not.
- Use == for value equality and is for object identity.