Understanding True and False: Boolean Expressions and Comparison
A boolean expression evaluates to True or False, which belong to the bool data type.
A Small Symbol with Two Jobs
Consider the expression 5 == 5. It does not store a value in a variable. Instead, it asks a question about the relationship between two operands: are their values equal? A boolean expression is an expression that evaluates to one of two values, True or False. Both values belong to Python's bool data type.
What do you think happens?
Before executing it, predict the result of 8 < 3.
Reveal answer
False
The left operand, 8, is not smaller than the right operand, 3. The comparison therefore produces the boolean value False.
Assignment and Comparison
The symbols = and == are not interchangeable. The = operator assigns a value. The == operator compares the values of two operands and produces True or False. A useful way to read them is: = performs an action on a variable, while == asks a question about two values. Using = where a comparison is intended is a common and critical error.
Reading the two operators
Compare the roles of score = 10 and score == 10.
- 1Assignment
The expression score = 10 assigns the value 10 to score. It is not a comparison and does not ask whether two values are equal.
- 2Comparison
The expression score == 10 compares the value of score with 10. If score contains 10, the expression produces True; otherwise it produces False.
- 3Prediction
After score has been assigned the value 10, score == 10 produces True because the compared values are equal.
Assignment changes or establishes a stored value; comparison produces a boolean result.
Eight Ways to Compare
Every comparison expression has a left operand, a comparison operator, and a right operand. Python provides eight comparison operators: ==, !=, >, <, >=, <=, is, and is not. Python examines the two operands, applies the operator's rule, and produces True or False.
| Operator | Relationship tested | Important reading |
|---|---|---|
| == | Value equality | Do the two operands have equal values? |
| != | Value inequality | Do the two operands have different values? |
| > | Strictly greater than | Is the left operand larger than the right operand, without treating equality as greater? |
| < | Strictly less than | Is the left operand smaller than the right operand, without treating equality as less? |
| >= | Greater than or equal to | Is the left operand larger than the right operand, or are the operands equal? |
| <= | Less than or equal to | Is the left operand smaller than the right operand, or are the operands equal? |
| is | Object identity | Do both operands refer to the exact same object in memory? |
| is not | Negated object identity | Do the operands not refer to the exact same object in memory? |
Each listed operator asks a different question and produces a boolean result.
Equality and Identity
The operators == and is look related, but they test different relationships. The == operator compares the values of two operands. The is operator checks whether the operands refer to the exact same object in memory. Two objects can contain equal values while still being different objects. For simple values such as numbers and strings, the distinction often does not matter; for more complex objects, == and is can produce different results.
Choosing the relationship
Suppose two operands contain equal values but refer to different objects. Predict the results of comparing them with == and is.
- 1Value comparison
The == operator asks whether the operands' values are equal. In this situation, the value comparison produces True.
- 2Identity comparison
The is operator asks whether both operands refer to the exact same object in memory. In this situation, they refer to different objects, so the identity comparison produces False.
- 3Operator choice
Use == when the relationship of interest is value equality. Use is when the relationship of interest is object identity.
Equal values can make == produce True while is produces False.
Predict Before Evaluating
To predict a comparison, read it from left to right in three steps. First identify the left operand. Then identify the relationship named by the operator. Finally compare the left and right operands and choose True or False. Pay special attention to equality: a pair of equal operands makes ==, >=, and <= true, but makes > and < false.
A complete prediction
Predict the result of 12 <= 12 without executing it.
- 1Identify the operands
The left operand is 12 and the right operand is 12.
- 2Read the operator
The operator <= means less than or equal to. It is inclusive, so equality is allowed.
- 3Compare
The operands are equal. Equality satisfies the inclusive <= comparison.
True
Predict the result of each expression before evaluating it: 4 != 9, 7 > 7, 3 < 8, and 5 is not 6. For each one, name the relationship being tested before choosing True or False.
Hints
- The != operator tests whether values are different.
- The > operator is exclusive, so equal operands do not satisfy it.
- The < operator asks whether the left operand is strictly smaller.
- The is not operator tests whether the operands are not the exact same object.
Mistakes to Avoid
Common mistake Using = when you intend to compare two values.
What it looks likeWriting score = 10 when the intended question is whether score has the value 10.
Why it failsThe = operator assigns a value; it does not perform a value comparison.
Do this insteadUse score == 10 when the intended result is True or False.
Common mistake Treating > and >= as interchangeable.
What it looks likeAssuming 6 > 6 is true because 6 is not smaller than 6.
Why it failsThe > operator is strictly greater than and does not include equality.
Do this insteadUse >= when equality should also produce True.
Common mistake Using is when the intended question concerns equal values.
What it looks likeChoosing is simply because two operands appear to contain the same value.
Why it failsThe is operator tests whether operands refer to the exact same object, not merely whether their values are equal.
Do this insteadUse == for value equality and reserve is for object identity.Common mistake Forgetting that every comparison produces a boolean result.
What it looks likeReading a comparison only as a relationship without identifying whether it evaluates to True or False.
Why it failsA comparison expression is evaluated by applying its operator to both operands and producing True or False.
Do this insteadState the relationship first, then predict the resulting boolean value.
Key Takeaways
- 1A boolean expression evaluates to True or False, and both values belong to the bool data type.
- 2The = operator assigns a value, while == compares operand values and produces a boolean result.
- 3Python provides eight comparison operators: ==, !=, >, <, >=, <=, is, and is not.
- 4The > and < operators exclude equality; >= and <= include equality.
- 5The == operator tests value equality, while is tests whether two operands refer to the exact same object.
At a glance
- Boolean expressions produce exactly one of two values: True or False.
- Assignment with = and comparison with == have different purposes.
- The eight comparison operators test value relationships, ordering relationships, or object identity.
- Equality is included by >= and <= but excluded by > and <.
- Use == for equal values and is for the exact same object.