Concepts / Comparison and Logical Operators

Comparison and Logical Operators

Comparison operators test relationships between values and return True or False, forming the foundation of conditional logic in Python.

  • Programming

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.

apply >relationship holdsapply ==relationship does not holdTwo values5 and 35 > 3comparisonTrueboolean result5 == 3comparisonFalseboolean result
How does comparing two values transform them into a single boolean result?

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.

checkcontinue through chaincombine comparison outcomesscoreone value0 <= scorelower boundBoolean resultrange conditionscore <= 100upper bound
How does Python evaluate a chained comparison such as 0 <= score <= 100?

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.

andornotTrue and Falsecombined conditionsFalseand resultTrue or Falsecombined conditionsTrueor resultnot Trueone conditionFalsenot result
How do and, or, and not combine True and False conditions?
Expression patternResult shown in the example
True and FalseFalse
True or FalseTrue
not TrueFalse

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.

place in intended groupplace in intended groupcombine conditionsproduceComparisonboolean resultGrouped conditionparenthesesLogical operatorand orBoolean resultcomplete expressionComparisonboolean result
How can parentheses make the intended evaluation structure visible in a mixed logical expression?

Mistakes That Change the Meaning

test relationshipused by mistake==equality comparisonTrue or Falsecomparison result=not the equality comparisonDifferent meaningcheck the operator
What is the difference between using == for an equality comparison and using = in a context where a comparison is intended?
  • 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

MEDIUM

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

  1. Comparison operators test relationships between values and return True or False.
  2. Chained comparisons such as 0 <= score <= 100 provide a concise way to test a range.
  3. The boolean operators and, or, and not combine multiple conditions into logical expressions.
  4. Parentheses make the intended grouping of complex logical expressions clearer.
  5. 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.