Concepts / Combining Boolean Expressions with and, or, and not

Combining Boolean Expressions with and, or, and not

A boolean expression evaluates to True or False, which belong to the bool data type.

  • Programming

From One Question to Several

A boolean expression is an expression that evaluates to True or False. These two values belong to Python's bool data type. A single comparison asks one question, such as whether one value is greater than another. Logical operators let you combine such questions into a larger boolean expression.

python

The expression above contains two comparisons joined by and. To understand the final result, evaluate each comparison first, then apply the logical operator that joins them.

How Logical Operators Combine Results

The operator and produces True only when both boolean expressions on its sides are True. The operator or produces True when at least one of its two expressions is True. The operator not reverses a boolean result: not True becomes False, and not False becomes True. Each smaller expression should be understood as producing a boolean value before you determine the result of the whole expression.

True and TrueTrueTrue and FalseFalseTrue or FalseTrueFalse or FalseFalsenot TrueFalsenot FalseTrue
How do pairs of True and False values combine with and, or, and not?
OperatorResult ruleExample result
andTrue only when both inputs are TrueTrue and False produces False
orTrue when at least one input is TrueTrue or False produces True
notReverses one boolean resultnot False produces True

Rules for combining boolean results

Tracing a Combined Expression

What do you think happens?

What value does this expression produce: 8 > 3 and 8 < 10?

  • True
  • False
Reveal answer

Answer: True

The comparison 8 > 3 is True, and the comparison 8 < 10 is also True. Because and requires both sides to be True, the combined expression produces True.

Checking a Range

Determine the result of 8 > 3 and 8 < 10.

Evaluate the left comparison: 8 is greater than 3, so 8 > 3 produces True.

Evaluate the right comparison: 8 is less than 10, so 8 < 10 produces True.

Apply and: Both inputs to and are True, so the combined expression produces True.

True

evaluate nexttwo resultscombined result8 > 3True8 < 10Trueandboth inputs TrueTrue
What value does the expression produce as evaluation moves through each comparison and the logical operator?
Output
True

Building the Comparisons

Every comparison expression has a left operand, a comparison operator, and a right operand. Python examines the two operands, applies the operator's rule, and produces True or False. These comparison results can then be combined with and, or, or not.

OperatorQuestion it asksExample
==Do the two operands have equal values?5 == 5
!=Do the two operands have different values?5 != 3
>Is the left operand strictly greater?5 > 3
<Is the left operand strictly smaller?3 < 5
>=Is the left operand greater or equal?5 >= 5
<=Is the left operand smaller or equal?5 <= 5
isDo the operands refer to the exact same object?first is second
is notDo the operands refer to different objects?first is not second

Python's eight comparison operators

python
Output (expected)
False
True
False
True

Assignment and Comparison

nameassigned valueleft operandright operandproducesscore=score==True or False55
What changes in program state when = assigns a value, compared with what happens when == compares two values?

The single equals sign, =, assigns a value. The double equals sign, ==, compares values and produces a boolean result. These operators are not interchangeable. Writing = when you intend a comparison is a common and critical error.

Equality and Identity

The operators == and is both compare two operands, but they ask different questions. The == operator compares the values of the operands. The is operator checks whether the operands refer to the exact same object in memory. Two objects can contain equal values without being the same object.

value comparisonvalue comparisonequal valuesidentity comparisonidentity comparisondifferent objectsObject Avalue: 10==equal valuesTrueObject Bvalue: 10issame object?False
How can two objects contain equal values without being the same object?
python
Output (expected)
True
False

In this example, first and second contain equal values, so == produces True. They are nevertheless separate objects, so is produces False. For simple values such as numbers and strings, the distinction often does not matter, but for more complex objects it can produce different results.

Mistakes to Avoid

  • Using = when you mean ==.

    The single equals sign assigns a value; it does not compare two values.

    Fix: Use score == 5 when you want a boolean comparison.

  • Treating > and >= as interchangeable.

    The > operator is exclusive and does not return True when the operands are equal.

    Fix: Use >= when equality should also count.

  • Using is to test whether two values are equal.

    The is operator tests whether two operands refer to the same object, not merely whether their values are equal.

    Fix: Use == for value equality.

  • Trying to decide the combined result before evaluating its component comparisons.

    A combined expression depends on the boolean result of each comparison.

    Fix: Evaluate each comparison first, then apply and, or, or not.

Practice Before Execution

MEDIUM

Predict the result of each expression before evaluating it: 1. 4 < 9 and 4 >= 4 2. 4 > 9 or 4 == 4 3. not (4 < 9) 4. 4 == 4 5. 4 is 4. For each answer, identify the comparison results first and then explain how the logical operator affects them.

Hints
  • The first expression uses and, so both comparisons must be True.
  • The second expression uses or, so one True comparison is enough.
  • The not operator reverses the result of the expression in parentheses.
  • Compare values with == and object identity with is.

One Practice Solution

Predict the result of 4 > 9 or 4 == 4.

Evaluate the first comparison: 4 is not greater than 9, so 4 > 9 produces False.

Evaluate the second comparison: 4 has the same value as 4, so 4 == 4 produces True.

Apply or: At least one input to or is True, so the combined expression produces True.

True

Reliable Evaluation Routine

  1. Separate the expression into its individual comparisons.
  2. Identify the left operand, operator, and right operand in each comparison.
  3. Check whether each comparison produces True or False.
  4. Apply and, or, or not to those boolean results.
  5. Check whether you used = for assignment or == for value comparison.
  6. If you used is or is not, confirm that you are asking about object identity rather than value equality.
  1. Boolean expressions produce True or False. Comparison expressions have a left operand, an operator, and a right operand. Use and when both conditions must be True, or when at least one condition may be True, and not to reverse a boolean result. Remember that = assigns, while == compares values. Python's eight comparison operators are ==, !=, >, <, >=, <=, is, and is not. Finally, == asks whether values are equal, while is asks whether two operands refer to the same object.

Key Takeaways

  • A boolean expression evaluates to one of two bool values: True or False.
  • Use and, or, and not to combine or reverse boolean results.
  • Use = for assignment and == for comparing values.
  • Python has eight comparison operators: ==, !=, >, <, >=, <=, is, and is not.
  • Use == for value equality and is for object identity.