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
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.
Assignment: The expression score = 10 assigns the value 10 to score. It is not a comparison and does not ask whether two values are equal.
Comparison: The expression score == 10 compares the value of score with 10. If score contains 10, the expression produces True; otherwise it produces False.
Prediction: 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? |
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.
Value comparison: The == operator asks whether the operands' values are equal. In this situation, the value comparison produces True.
Identity 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.
Operator 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.
Identify the operands: The left operand is 12 and the right operand is 12.
Read the operator: The operator <= means less than or equal to. It is inclusive, so equality is allowed.
Compare: 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
Using = when you intend to compare two values.
The = operator assigns a value; it does not perform a value comparison.
Fix:
Use score == 10 when the intended result is True or False.Treating > and >= as interchangeable.
The > operator is strictly greater than and does not include equality.
Fix:
Use >= when equality should also produce True.Using is when the intended question concerns equal values.
The is operator tests whether operands refer to the exact same object, not merely whether their values are equal.
Fix:
Use == for value equality and reserve is for object identity.Forgetting that every comparison produces a boolean result.
A comparison expression is evaluated by applying its operator to both operands and producing True or False.
Fix:
State the relationship first, then predict the resulting boolean value.
Key Takeaways
- A boolean expression evaluates to True or False, and both values belong to the bool data type.
- The = operator assigns a value, while == compares operand values and produces a boolean result.
- Python provides eight comparison operators: ==, !=, >, <, >=, <=, is, and is not.
- The > and < operators exclude equality; >= and <= include equality.
- The == operator tests value equality, while is tests whether two operands refer to the exact same object.
Key Takeaways
- 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.