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.

Two values5 and 35 > 3comparisonTrueboolean result5 == 3comparisonFalseboolean resultapply >relationship holdsapply ==relationship does not hold
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.

Worked example

Evaluating a Single Comparison

Determine the result of 5 > 3 and 5 == 3.

  1. 1
    First comparison

    The relationship 5 > 3 holds, so the result is True.

  2. 2
    Second comparison

    The relationship 5 == 3 does not hold, so the result is False.

  3. 3
    Interpret the result

    Each comparison has been reduced to a boolean value that can be used in logical expressions.

Result

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.

scoreone value0 <= scorelower boundBoolean resultrange conditionscore <= 100upper boundcheckcontinue through chaincombine comparison outcomes
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.

True and Falsecombined conditionsFalseand resultTrue or Falsecombined conditionsTrueor resultnot Trueone conditionFalsenot resultandornot
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.

Comparisonboolean resultGrouped conditionparenthesesLogical operatorand orBoolean resultcomplete expressionComparisonboolean resultplace in intended groupplace in intended groupcombine conditionsproduce
How can parentheses make the intended evaluation structure visible in a mixed logical expression?

Mistakes That Change the Meaning

==equality comparisonTrue or Falsecomparison result=not the equality comparisonDifferent meaningcheck the operatortest relationshipused by mistake
What is the difference between using == for an equality comparison and using = in a context where a comparison is intended?
  • Common mistake

    Using = when the intended operation is an equality comparison.

    What it looks like
    Writing = instead of == when checking whether two values are equal.
    Why it fails

    The two symbols are not interchangeable, so the expression does not communicate the intended equality test.

    Do this instead
    Use == for the equality comparison described by the condition.
  • Common mistake

    Treating a chained comparison as unrelated pieces without checking the complete range condition.

    What it looks like

    Reading 0 <= score <= 100 without considering both boundary relationships.

    Why it fails

    The value must be considered against the lower and upper bounds represented in the chain.

    Do this instead

    Trace each comparison in the chain and then determine the resulting boolean condition.

  • Common mistake

    Combining conditions without making their intended grouping clear.

    What it looks like

    Writing a long expression that mixes comparisons, and, or, and not without parentheses.

    Why it fails

    The reader may misunderstand which conditions are meant to be combined first.

    Do this instead

    Use parentheses to show the intended logical structure.

  • Common mistake

    Confusing logical operators with bitwise operators.

    What it looks like

    Choosing a bitwise operation when the task is to combine boolean conditions.

    Why it fails

    Bitwise operators manipulate the binary representations of numbers, while boolean operators combine conditions.

    Do this instead

    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.
Worked example

A Complete Trace

Predict the result of a range check followed by a logical combination.

  1. 1
    Identify the comparison

    The range expression 0 <= score <= 100 is a chained comparison with a lower and an upper boundary.

  2. 2
    Reduce the comparison

    The chained comparison becomes one boolean result after the boundary relationships are considered.

  3. 3
    Combine conditions

    If another condition is present, use and, or, or not to combine or transform the boolean results.

  4. 4
    Check readability

    If several logical operators are present, add parentheses so the intended grouping is clear.

Result

A reliable evaluation moves from value relationships to boolean results, then combines those results with explicit logical structure.

Key Takeaways

  1. 1
    Comparison operators test relationships between values and return True or False.
  2. 2
    Chained comparisons such as 0 <= score <= 100 provide a concise way to test a range.
  3. 3
    The boolean operators and, or, and not combine multiple conditions into logical expressions.
  4. 4
    Parentheses make the intended grouping of complex logical expressions clearer.
  5. 5
    Do not confuse equality comparison with =, and do not confuse boolean operators with bitwise operators.

At a glance

  • 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.

Related concepts

More concepts starting with C