← Home

023. Comparison Operators

Predicates that return boolean values

023. Comparison Operators

RAM Manager — Alert Severity Levels

Aryan’s RAM manager needs to classify current memory pressure into severity levels so the dashboard can color-code alerts:

Usage %Level
< 50%“normal”
50–74%“warning”
75–89%“critical”
>= 90%“emergency”

He writes classify_usage(used_pct), but the boundary between “warning” and “critical” is wrong — his code uses < 70 where it should be < 75. So a process at 72% usage is incorrectly classified as “critical” instead of “warning”, firing unnecessary pages.

The lesson: Off-by-a-few comparisons are silent bugs. The code runs, returns a value, and looks plausible — but the boundary is in the wrong place. Always verify the exact threshold values against the spec.


💡 Fun fact: Python supports chained comparisons natively: 0 <= x < 10 is valid Python and evaluates correctly without parentheses. In C, you would need x >= 0 && x < 10. Python’s chaining mirrors standard mathematical notation and is unique among mainstream programming languages.

⚠️ Watch out: The single most common comparison mistake is using = (assignment) instead of == (equality check) inside an if condition. In C this produces a silent logic error; Python raises a SyntaxError for if x = 5:, which saves you from this particular trap — but wrong boundary values (like < 10 instead of < 15) are completely silent.

🤔 Think about it: Once an if celsius < 0 check passes, you know celsius is negative in the else branch. How does Python’s elif cascade let you simplify subsequent conditions, and why does removing redundant checks make code easier to read?

Learning objectives

  • Use ==, !=, <, >, <=, >= for comparisons
  • [object Object]
  • Simplify elif conditions by relying on prior checks

Key concepts

  • comparison operators
  • ==
  • <
  • =

Try it

Concept detail

Python comparison operators return boolean values (True/False): == (equal), != (not equal), < (less), > (greater), <= (less or equal), >= (greater or equal). Python allows chaining: 0 <= x < 10 is the same as (0 <= x) and (x < 10). Common mistake: using = (assignment) when == (comparison) is needed. After an if checks one condition, subsequent elif/else runs only when previous conditions were False, so you don’t need to re-check. This simplifies conditions.

Solution

def classify_temp(celsius):
    if celsius < 0:
        return "freezing"
    elif celsius < 15:
        return "cold"
    elif celsius < 25:
        return "comfortable"
    else:
        return "hot"

Tests

def test_freezing():
    assert classify_temp(-5) == "freezing"
    assert classify_temp(-0.1) == "freezing"

def test_cold():
    assert classify_temp(0) == "cold"
    assert classify_temp(10) == "cold"
    assert classify_temp(14) == "cold"

def test_comfortable():
    assert classify_temp(15) == "comfortable"
    assert classify_temp(20) == "comfortable"
    assert classify_temp(24) == "comfortable"

def test_hot():
    assert classify_temp(25) == "hot"
    assert classify_temp(40) == "hot"

Resources