Codectionary / Developer documentation / Python

Comparison & Logical Operators

Comparisons test relationships between values; Boolean operators combine decisions. Comparisons between the ordinary built-in values used here return True or False. and and or short-circuit and return one of their operands; not always returns a Boolean.

How it works

== compares values, while is compares object identity. Two separate lists can be equal without being the same object. Use is None for the absence-of-value sentinel, not is when comparing strings or numbers.

a and b evaluates b only when a is truthy; otherwise it returns a. a or b evaluates b only when a is falsy; otherwise it returns a. For example, "" or "Guest" returns "Guest". Convert with bool() if the surrounding API specifically needs True or False.

A chained comparison such as 0 <= score <= 100 tests both bounds, evaluating the middle expression once. not binds more tightly than and, and and more tightly than or. Parentheses can make mixed conditions easier to read.

Common mistakes and boundaries

Use == to compare, not =, which assigns. Python uses and/or, not &&/||.

A fallback such as value or default replaces every falsy value, including a valid zero. Use an explicit None check when only missing values should receive a default.

User-defined comparison methods can return non-Boolean objects. Libraries such as array libraries may require explicit elementwise operations; do not generalise the simple scalar examples to every object.

Try it yourself

Predict the values of 0 or 10, "ready" and 5, and not "". Then write a guard that checks a list is nonempty before reading its first item.

Syntax

==  !=  <  >  <=  >=   and  or  not

Examples

Comparison Operators

Comparing values to produce a boolean result.

print(5 == 5)   # True
print(5 != 3)   # True
print(5 > 3)    # True
print(5 <= 4)   # False

print("abc" == "abc")  # True
print("abc" < "abd")   # True (lexicographic comparison)
  1. These numeric and string comparisons return Booleans. String ordering compares Unicode code points lexicographically, not human-language dictionary order.
  2. Expected output: True True True False True True

Logical Operators

Combining boolean expressions with and, or, and not.

age = 25
has_id = True

if age >= 18 and has_id:
    print("Entry allowed")

is_weekend = False
is_holiday = True

if is_weekend or is_holiday:
    print("No work today")

is_banned = False
if not is_banned:
    print("Access granted")
  1. Each branch combines simple Boolean decisions. Short-circuiting avoids evaluating an unnecessary right operand.
  2. Expected output: Entry allowed No work today Access granted

Chained Comparisons

Writing range checks concisely by chaining comparison operators.

age = 25

# Instead of: age >= 18 and age <= 65
if 18 <= age <= 65:
    print("Working age")

score = 72
grade = "Pass" if 50 <= score < 100 else "Check score"
print(grade)
  1. Chained bounds avoid repeating the middle expression. This example intentionally excludes 100; adjust the upper comparison if 100 is a valid score.
  2. Expected output: Working age Pass

Best practices

  • Use 'and'/'or'/'not' rather than symbolic operators - Python doesn't support && or ||
  • Take advantage of chained comparisons (0 < x < 10) instead of writing (x > 0 and x < 10)
  • Remember that 'and'/'or' short-circuit - the second operand isn't evaluated if the first already determines the result
  • Use 'is'/'is not' for identity checks (like comparing to None), and '=='/'!=' for value equality

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading