Boolean Expressions and Comparison Operators
A Boolean expression is an expression whose result is either True or False.
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?
Reveal 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.
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
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.
- 1First comparison
In 4 == 4, the two operands have the same value, so == produces True.
- 2Second 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).
- 1Evaluate the comparison
Begin by evaluating x > y. The comparison produces either True or False.
- 2Apply 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
Common mistake Treating a comparison as if it were just a pair of values.
What it looks likeReading 2 == 3 without identifying == as the operator.
Why it failsAn expression is made from operators and operands. The operator determines how the operands are related and what result is produced.
Do this insteadName 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 likeAssuming 4 == 5 is True because it is written as a Boolean expression.
Why it failsA Boolean expression can produce either True or False.
Do this insteadCheck the relationship between the operands. == produces True only when the operands are equal.
Common mistake Forgetting that not changes the result.
What it looks likeTreating not (x > y) as having the same result as x > y.
Why it failsThe not operator negates a Boolean expression.
Do this insteadEvaluate 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 likeIgnoring the source rule that a nonzero number is interpreted as true by Python.
Why it failsPython is not very strict about the operands of logical operators.
Do this insteadRemember that logical operators should receive Boolean expressions, while Python also interprets any nonzero number as true.
Practice and Verification
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.