Comparison and Logical Operators
Comparison operators test relationships between values and return True or False, forming the foundation of conditional logic in Python.
From Values to Decisions
A comparison operator turns a relationship between values into a boolean result: True or False. That result can then support conditional logic. Logical operators extend this idea by combining multiple conditions into a larger expression.
Reading Comparison Results
A comparison asks whether a relationship between values is true. The answer is always expressed as one of two boolean results: True or False. For example, the relationship in 5 > 3 is true, while the relationship in 5 == 3 is false. The important mental step is to separate the values from the result: the comparison examines values, but the expression produces a boolean result that can participate in conditional logic.
Evaluating a Single Comparison
Determine the result of 5 > 3 and 5 == 3.
First comparison: The relationship 5 > 3 holds, so the result is True.
Second comparison: The relationship 5 == 3 does not hold, so the result is False.
Interpret the result: Each comparison has been reduced to a boolean value that can be used in logical expressions.
5 > 3 produces True, and 5 == 3 produces False.
Tracing Chained Comparisons
A chained comparison places multiple comparison relationships in one expression. The source gives 0 <= score <= 100 as a concise and readable way to test whether a value falls within a range. To understand the result, inspect each relationship in the chain and then consider whether the complete range condition holds.
What do you think happens?
What should the chained expression 0 <= score <= 100 communicate when the value of score is within the stated range?
Reveal answer
Answer: It communicates a True range condition when both boundary relationships hold.
The expression is useful because it represents the lower-bound and upper-bound tests together in a concise, readable form.
Combining Boolean Conditions
The boolean operators and, or, and not combine conditions into complex logical expressions. Each individual condition can produce True or False, and the logical expression combines those boolean results into a result that can control program flow.
| Expression pattern | Result shown in the example |
|---|---|
| True and False | False |
| True or False | True |
| not True | False |
These examples show how boolean operators combine or transform boolean conditions.
Making Grouping Explicit
Logical expressions can contain comparisons together with and, or, and not. When several operators appear in one expression, make the intended grouping clear rather than asking the reader to guess how the expression should be understood. Parentheses are a practical way to show which conditions belong together. This article focuses on explicit grouping instead of relying on an unstated precedence rule.
Mistakes That Change the Meaning
Using = when the intended operation is an equality comparison.
The two symbols are not interchangeable, so the expression does not communicate the intended equality test.
Fix:
Use == for the equality comparison described by the condition.Treating a chained comparison as unrelated pieces without checking the complete range condition.
The value must be considered against the lower and upper bounds represented in the chain.
Fix:
Trace each comparison in the chain and then determine the resulting boolean condition.Combining conditions without making their intended grouping clear.
The reader may misunderstand which conditions are meant to be combined first.
Fix:
Use parentheses to show the intended logical structure.Confusing logical operators with bitwise operators.
Bitwise operators manipulate the binary representations of numbers, while boolean operators combine conditions.
Fix:
Use and, or, and not for boolean conditions; treat bitwise operations as a separate topic.
Practice Before Coding
For each expression, predict the boolean result and explain the comparison or logical step that determines it: 5 > 3; 5 == 3; 0 <= score <= 100 when score is within the stated range; True and False; True or False; not True. Then rewrite one mixed expression with parentheses so its intended grouping is explicit.
Hints
- Start by evaluating each comparison as True or False.
- For a chained comparison, inspect both boundary relationships.
- For a logical expression, identify whether it uses and, or, or not.
- Use parentheses when you want the structure of a mixed expression to be immediately visible.
A Complete Trace
Predict the result of a range check followed by a logical combination.
Identify the comparison: The range expression 0 <= score <= 100 is a chained comparison with a lower and an upper boundary.
Reduce the comparison: The chained comparison becomes one boolean result after the boundary relationships are considered.
Combine conditions: If another condition is present, use and, or, or not to combine or transform the boolean results.
Check readability: If several logical operators are present, add parentheses so the intended grouping is clear.
A reliable evaluation moves from value relationships to boolean results, then combines those results with explicit logical structure.
Key Takeaways
- Comparison operators test relationships between values and return True or False.
- Chained comparisons such as 0 <= score <= 100 provide a concise way to test a range.
- The boolean operators and, or, and not combine multiple conditions into logical expressions.
- Parentheses make the intended grouping of complex logical expressions clearer.
- Do not confuse equality comparison with =, and do not confuse boolean operators with bitwise operators.
Key Takeaways
- Comparisons turn relationships between values into True or False.
- Chained comparisons express range checks concisely and should be traced across all their relationships.
- and, or, and not combine boolean conditions into expressions that can control program flow.
- Use parentheses to make complex logical grouping clear.
- Use == for equality comparisons and keep boolean operators distinct from bitwise operators.