Concepts / Conditional Statements: if, elif, and else

Conditional Statements: if, elif, and else

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

  • Programming

A Decision Begins with a Boolean

A conditional statement needs a decision. In Python, that decision can come from a boolean expression: an expression that evaluates to either True or False. True and False belong to the bool data type. A conditional uses this result to determine which branch of the decision should run.

A boolean expression evaluates to True or False, and both values belong to the bool data type.

TrueFalseTrueFalseif conditionTrue or Falseif branchelif branchelif conditionchecked when neededelse branch
Which condition is evaluated next, and which code branch runs when each condition is True or False?

Reading a Comparison

A comparison expression has three parts: a left operand, a comparison operator, and a right operand. Python examines the two operands, applies the operator's rule, and produces True or False. That boolean result can then serve as the decision for a conditional statement.

Predicting an Equal Comparison

Predict the result of the comparison 5 == 5 before Python evaluates it.

Identify the operands: The left operand is 5 and the right operand is 5.

Identify the operator: The == operator compares the values of the two operands.

Compare the values: Both operands have the value 5, so the values are equal.

The comparison produces True.

What do you think happens?

What result should you predict for 7 > 7 before execution?

  • True
  • False
Reveal answer

Answer: False

The > operator is exclusive: it is True only when the left operand is strictly larger than the right operand. Equal operands do not satisfy >.

OperatorRelationship tested
==Whether the two operand values are equal
!=Whether the two operand values are not equal
>Whether the left operand is strictly larger
<Whether the left operand is strictly smaller
>=Whether the left operand is larger or equal
<=Whether the left operand is smaller or equal
isWhether both operands refer to the exact same object
is notWhether both operands do not refer to the exact same object

Python's eight comparison operators produce a boolean result.

The distinction between exclusive and inclusive comparisons is important. The > and < operators require a strict difference: the left value must be larger or smaller, respectively. The >= and <= operators also accept equality. Therefore, when the operands are equal, >= and <= can be True, while > and < are False.

Storing Versus Comparing

storesproducesscorename5left operand=assigns==compares5value5right operandTrueboolean result
Does the symbol store a value in a variable, or compare two values and produce a boolean result?

The single equals sign and the double equals sign have different jobs. The = operator assigns a value. The == operator compares values and produces True or False. Using = where a comparison is required is a common and critical error.

  • Treating = as a comparison operator

    In Python, = assigns a value; it does not compare two values.

    Fix: Use == when the condition must compare values.

  • Assuming > includes equality

    The > operator is exclusive and requires the left operand to be strictly larger.

    Fix: Use >= when equality should also satisfy the comparison.

  • Assuming == and is ask the same question

    == compares values, while is checks whether the operands refer to the exact same object.

    Fix: Choose == for value equality and is for object identity.

Equality and Identity

The == and is operators can look interchangeable because both participate in comparisons, but they test different relationships. == compares the values of two operands. is checks whether the operands refer to the exact same object in memory. Two objects can contain equal values while still being different objects, so value equality and object identity can produce different results for complex objects.

compared withcompared withchecked withchecked withObject Avalue: equal==value comparisonObject Bvalue: equalissame-object test
How can two objects contain equal values while still being different objects in memory?

Predict Before Execution

Prediction is a useful habit because every comparison follows the same structure. First identify the operands. Then identify the operator. Finally apply that operator's rule and write down True or False. This process separates reasoning about the comparison from the later execution of the program.

EASY

For each expression, predict whether the result is True or False: 4 < 9, 9 <= 9, 10 != 10, and 3 >= 8. Then explain which operator rule led to each prediction.

Hints
  • The < operator requires the left operand to be strictly smaller.
  • The <= operator accepts equality.
  • The != operator tests whether values are different.
  • The >= operator accepts a larger left operand or equal operands.

A Four-Expression Check

Predict the results of 4 < 9, 9 <= 9, 10 != 10, and 3 >= 8.

4 < 9: Four is strictly smaller than nine, so the result is True.

9 <= 9: The operands are equal, and <= includes equality, so the result is True.

10 != 10: The operands have equal values, so they are not unequal; the result is False.

3 >= 8: Three is neither larger than nor equal to eight, so the result is False.

The predicted results are True, True, False, and False, in that order.

Decision-Making Checklist

  1. Identify the boolean expression that supplies the conditional decision.
  2. Check whether the expression produces True or False.
  3. Use == to compare values and = to assign values.
  4. Remember that > and < are exclusive, while >= and <= include equality.
  5. Use == for value equality and is for exact object identity.
  6. Before execution, identify the operands and operator and predict the boolean result.

Key Takeaways

  • A boolean expression evaluates to True or False, and both values belong to the bool data type.
  • A comparison expression contains a left operand, an operator, and a right operand, and produces a boolean result.
  • The = operator assigns a value, while == compares values.
  • The > and < operators are exclusive; >= and <= also accept equal operands.
  • The == operator tests value equality, while is tests whether two operands refer to the exact same object.