Concepts / Boolean Expressions and Comparison Operators

Boolean Expressions and Comparison Operators

A Boolean expression is an expression whose result is either True or False.

  • Programming

Expressions as Evaluated Statements

Most statements, or logical lines, that you write contain expressions. An expression is something that can be evaluated to produce a result. The simple expression 2 + 3 contains an operator and operands: + is the operator, while 2 and 3 are the operands. Boolean expressions follow the same general structure, but their result is either True or False.

Key point

A Boolean expression is an expression whose result is either True or False.

Tracing a Comparison

What do you think happens?

What result should the comparison 2 == 3 produce?

  • True

  • False

Reveal answer
Answer:

False

The == operator compares its two operands and produces True when they are equal. Because 2 and 3 are not equal, the result is False.

2left operand==comparison operator3right operandFalsecomparison resultoperandcompares withproduces
How do two operand values and a comparison operator combine to produce either True or False?

Read a comparison from its parts. The operands are the values being compared, and the operator specifies the comparison. With ==, the two operands are compared for equality. The result is True if they are equal and False otherwise. In 2 == 3, the operands are 2 and 3, and the result is False.

Evaluating Boolean Conditions

Boolean expressionoperands and operatorEvaluateapply the operatorTruecondition is trueFalsecondition is falseinspectwhen condition holdsotherwise
How does an expression represent a condition whose result can be evaluated as True or False?

Evaluation turns an expression into a result. For a Boolean expression, that result has only two possibilities in the examples described here: True or False. The comparison operator examines its operands and determines which Boolean result to produce.

Worked example

Comparing Equal and Unequal Operands

Evaluate 4 == 4 and 4 == 5.

  1. 1
    First comparison

    In 4 == 4, the two operands have the same value, so == produces True.

  2. 2
    Second comparison

    In 4 == 5, the two operands do not have the same value, so == produces False.

Result

4 == 4 produces True, while 4 == 5 produces False.

The same tracing method applies when the comparison uses another comparison form shown in the source, such as x > y. First identify the operands, then apply the operator, and finally identify whether the Boolean expression is True or False. When the expression is placed after not, its Boolean result is reversed: not (x > y) is true if x > y is false.

Combining Boolean Results

Definition

A logical operator combines Boolean expressions. The logical operators described here are and, or, and not.

The operators and and or combine Boolean expressions. The not operator negates a Boolean expression. For example, not (x > y) is true when x > y is false. This means that not changes the Boolean result of the expression it applies to.

Worked example

Following the not Operator

Trace the relationship between x > y and not (x > y).

  1. 1
    Evaluate the comparison

    Begin by evaluating x > y. The comparison produces either True or False.

  2. 2
    Apply not

    The not operator negates that Boolean expression. If x > y is false, not (x > y) is true.

Result

not reverses the Boolean result of the expression inside the parentheses.

Mistakes in Evaluation

  • Common mistake

    Treating a comparison as if it were just a pair of values.

    What it looks like

    Reading 2 == 3 without identifying == as the operator.

    Why it fails

    An expression is made from operators and operands. The operator determines how the operands are related and what result is produced.

    Do this instead

    Name each part: 2 and 3 are operands, == is the comparison operator, and False is the Boolean result.

  • Common mistake

    Assuming every comparison produces True.

    What it looks like

    Assuming 4 == 5 is True because it is written as a Boolean expression.

    Why it fails

    A Boolean expression can produce either True or False.

    Do this instead

    Check the relationship between the operands. == produces True only when the operands are equal.

  • Common mistake

    Forgetting that not changes the result.

    What it looks like

    Treating not (x > y) as having the same result as x > y.

    Why it fails

    The not operator negates a Boolean expression.

    Do this instead

    Evaluate the comparison first, then reverse its Boolean result when applying not.

  • Common mistake

    Assuming logical operators accept only explicitly written Boolean values.

    What it looks like

    Ignoring the source rule that a nonzero number is interpreted as true by Python.

    Why it fails

    Python is not very strict about the operands of logical operators.

    Do this instead

    Remember that logical operators should receive Boolean expressions, while Python also interprets any nonzero number as true.

Practice and Verification

EASY

For each expression, identify the operands, identify the operator, and state whether the result is True or False when the source gives enough information to determine it: 2 == 2, 2 == 5, and not (x > y) when x > y is false.

Hints
  • Start by separating each expression into its operands and operator.
  • The == operator produces True when its operands are equal and False otherwise.
  • For not (x > y), first use the stated result of x > y, then negate it.
Key point

A reliable evaluation routine is: separate the operands from the operator, apply the operator, record the Boolean result, and then apply not, and, or, or any other logical operator that surrounds the expression.

Key Takeaways

  • An expression contains operators and operands and can be evaluated to produce a result.
  • A Boolean expression produces either True or False.
  • The == operator compares two operands and produces True when they are equal and False otherwise.
  • The logical operators and, or, and not combine or negate Boolean expressions.
  • The not operator reverses a Boolean result, and Python interprets any nonzero number as true when used as an operand of a logical operator.

More concepts starting with B