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
Interactive lab

Try it: if / elif / else

How Python tests if / elif conditions from the top, runs only the first true branch, and how and / or stop evaluating as soon as the answer is known (short-circuiting).

How it works

  1. Python evaluates the if condition first.
  2. If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
  3. If none is True, the else branch runs.
  4. and stops at the first False, or stops at the first True — the right side may never be evaluated.

Default run (5 steps): score = 84; A = 90, B = 80, C = 70. Python tests the conditions from the top. … print(grade) shows B.

Educational simulation

Loading the simulation…

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
Notes on the code
  • temperature > 0 produces one boolean result
  • temperature < 30 produces another boolean result
  • and combines the two results

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.

Worked example

Checking a Range

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

  1. 1
    Evaluate the left comparison

    8 is greater than 3, so 8 > 3 produces True.

  2. 2
    Evaluate the right comparison

    8 is less than 10, so 8 < 10 produces True.

  3. 3
    Apply and

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

Result

True

8 > 3True8 < 10Trueandboth inputs TrueTrueevaluate nexttwo resultscombined result
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

score=score==True or False55nameassigned valueleft operandright operandproduces
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.

Before
score = 5

The name score is assigned the value 5.

After
score == 5

The value associated with score is compared with 5, producing True or False.

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.

Object Avalue: 10==equal valuesTrueObject Bvalue: 10issame object?Falsevalue comparisonvalue comparisonequal valuesidentity comparisonidentity comparisondifferent objects
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

  • Common mistake

    Using = when you mean ==.

    What it looks like
    score = 5
    Why it fails

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

    Do this instead

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

  • Common mistake

    Treating > and >= as interchangeable.

    What it looks like
    5 > 5
    Why it fails

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

    Do this instead

    Use >= when equality should also count.

  • Common mistake

    Using is to test whether two values are equal.

    What it looks like

    first is second

    Why it fails

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

    Do this instead
    Use == for value equality.
  • Common mistake

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

    What it looks like
    8 > 3 and 8 < 10
    Why it fails

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

    Do this instead

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

One Practice Solution

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

  1. 1
    Evaluate the first comparison

    4 is not greater than 9, so 4 > 9 produces False.

  2. 2
    Evaluate the second comparison

    4 has the same value as 4, so 4 == 4 produces True.

  3. 3
    Apply or

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

Result

True

Reliable Evaluation Routine

  1. 1
    Separate the expression into its individual comparisons.
  2. 2
    Identify the left operand, operator, and right operand in each comparison.
  3. 3
    Check whether each comparison produces True or False.
  4. 4
    Apply and, or, or not to those boolean results.
  5. 5
    Check whether you used = for assignment or == for value comparison.
  6. 6
    If you used is or is not, confirm that you are asking about object identity rather than value equality.
  1. 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.

Related concepts

More concepts starting with C