Concepts / Boolean Expressions and Comparison Operators

Boolean Expressions and Comparison Operators

Most statements (logical lines) that you write will contain expressions . A simple example of an expression is 2 + 3 . An expression can be broken down into operators and operands.

  • 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.

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.

operandcompares withproduces2left operand==comparison operator3right operandFalsecomparison result
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

inspectwhen condition holdsotherwiseBoolean expressionoperands and operatorEvaluateapply the operatorTruecondition is trueFalsecondition is false
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.

Comparing Equal and Unequal Operands

Evaluate 4 == 4 and 4 == 5.

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

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

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

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.

Following the not Operator

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

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

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

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

Mistakes in Evaluation

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

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

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

  • Assuming every comparison produces True.

    A Boolean expression can produce either True or False.

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

  • Forgetting that not changes the result.

    The not operator negates a Boolean expression.

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

  • Assuming logical operators accept only explicitly written Boolean values.

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

    Fix: 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.

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.