Concepts / Understanding True and False: Boolean Expressions and Comparison

Understanding True and False: Boolean Expressions and Comparison

A boolean expression evaluates to True or False, which belong to the bool data type.

  • Programming

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.

  • True

  • False

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.

scoreleft operandscoreleft operand=assigns a value==compares values10assigned value10right operandTrueboolean resultevaluates to
What does each operator do, and how does the program state change when = is used compared with the True or False result produced by ==?
Worked example

Reading the two operators

Compare the roles of score = 10 and score == 10.

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

  2. 2
    Comparison

    The expression score == 10 compares the value of score with 10. If score contains 10, the expression produces True; otherwise it produces False.

  3. 3
    Prediction

    After score has been assigned the value 10, score == 10 produces True because the compared values are equal.

Result

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.

OperatorRelationship testedImportant reading
==Value equalityDo the two operands have equal values?
!=Value inequalityDo the two operands have different values?
>Strictly greater thanIs the left operand larger than the right operand, without treating equality as greater?
<Strictly less thanIs the left operand smaller than the right operand, without treating equality as less?
>=Greater than or equal toIs the left operand larger than the right operand, or are the operands equal?
<=Less than or equal toIs the left operand smaller than the right operand, or are the operands equal?
isObject identityDo both operands refer to the exact same object in memory?
is notNegated object identityDo the operands not refer to the exact same object in memory?

Each listed operator asks a different question and produces a boolean result.

==equal valuesTrue or Falsebool result!=different values>strictly larger<strictly smaller>=larger or equal<=smaller or equalissame objectis notdifferent objectproducesproducesproducesproducesproducesproducesproducesproduces
How do the eight operators differ in the relationship they test while producing the same kind of 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.

Object Avalue: equal==equal valuesObject Bvalue: equalissame object onlymay satisfymay satisfyteststests
How can two objects contain equal values but still be different objects in memory?
Worked example

Choosing the relationship

Suppose two operands contain equal values but refer to different objects. Predict the results of comparing them with == and is.

  1. 1
    Value comparison

    The == operator asks whether the operands' values are equal. In this situation, the value comparison produces True.

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

  3. 3
    Operator choice

    Use == when the relationship of interest is value equality. Use is when the relationship of interest is object identity.

Result

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.

6 >= 6two equal operandsTrueinclusive comparisonevaluates to
Given two values and a comparison operator, what True or False result should be produced before the expression is executed?
Worked example

A complete prediction

Predict the result of 12 <= 12 without executing it.

  1. 1
    Identify the operands

    The left operand is 12 and the right operand is 12.

  2. 2
    Read the operator

    The operator <= means less than or equal to. It is inclusive, so equality is allowed.

  3. 3
    Compare

    The operands are equal. Equality satisfies the inclusive <= comparison.

Result

True

MEDIUM

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 like

    Writing score = 10 when the intended question is whether score has the value 10.

    Why it fails

    The = operator assigns a value; it does not perform a value comparison.

    Do this instead

    Use score == 10 when the intended result is True or False.

  • Common mistake

    Treating > and >= as interchangeable.

    What it looks like

    Assuming 6 > 6 is true because 6 is not smaller than 6.

    Why it fails

    The > operator is strictly greater than and does not include equality.

    Do this instead

    Use >= when equality should also produce True.

  • Common mistake

    Using is when the intended question concerns equal values.

    What it looks like

    Choosing is simply because two operands appear to contain the same value.

    Why it fails

    The is operator tests whether operands refer to the exact same object, not merely whether their values are equal.

    Do this instead
    Use == for value equality and reserve is for object identity.
  • Common mistake

    Forgetting that every comparison produces a boolean result.

    What it looks like

    Reading a comparison only as a relationship without identifying whether it evaluates to True or False.

    Why it fails

    A comparison expression is evaluated by applying its operator to both operands and producing True or False.

    Do this instead

    State the relationship first, then predict the resulting boolean value.

Key Takeaways

  1. 1
    A boolean expression evaluates to True or False, and both values belong to the bool data type.
  2. 2
    The = operator assigns a value, while == compares operand values and produces a boolean result.
  3. 3
    Python provides eight comparison operators: ==, !=, >, <, >=, <=, is, and is not.
  4. 4
    The > and < operators exclude equality; >= and <= include equality.
  5. 5
    The == 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.

Related concepts

More concepts starting with U