Concepts / Conditional Statements: if, elif, and else

Conditional Statements: if, elif, and else

A conditional statement needs a decision.

  • Programming
Interactive lab

Try it: if / elif / else

How Python tests if / elif conditions from the top, runs only the first true branch, and how and / or stop evaluating as soon as the answer is known (short-circuiting).

How it works

  1. Python evaluates the if condition first.
  2. If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
  3. If none is True, the else branch runs.
  4. and stops at the first False, or stops at the first True — the right side may never be evaluated.

Default run (5 steps): score = 84; A = 90, B = 80, C = 70. Python tests the conditions from the top. … print(grade) shows B.

Educational simulation

Loading the simulation…

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.

Definition

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

if conditionTrue or Falseif branchelif branchelif conditionchecked when neededelse branchTrueFalseTrueFalse
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.

Worked example

Predicting an Equal Comparison

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

  1. 1
    Identify the operands

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

  2. 2
    Identify the operator

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

  3. 3
    Compare the values

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

Result

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

scorename5left operand=assigns==compares5value5right operandTrueboolean resultstoresproduces
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.

  • Common mistake

    Treating = as a comparison operator

    What it looks like
    Using = when the conditional needs to ask whether two values are equal.
    Why it fails

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

    Do this instead
    Use == when the condition must compare values.
  • Common mistake

    Assuming > includes equality

    What it looks like

    Expecting a comparison between equal values to make > True.

    Why it fails

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

    Do this instead

    Use >= when equality should also satisfy the comparison.

  • Common mistake

    Assuming == and is ask the same question

    What it looks like

    Using is when the intended test concerns equal values.

    Why it fails

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

    Do this instead
    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.

Object Avalue: equal==value comparisonObject Bvalue: equalissame-object testcompared withcompared withchecked withchecked with
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.
Worked example

A Four-Expression Check

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

  1. 1
    4 < 9

    Four is strictly smaller than nine, so the result is True.

  2. 2
    9 <= 9

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

  3. 3
    10 != 10

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

  4. 4
    3 >= 8

    Three is neither larger than nor equal to eight, so the result is False.

Result

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

Decision-Making Checklist

  1. 1
    Identify the boolean expression that supplies the conditional decision.
  2. 2
    Check whether the expression produces True or False.
  3. 3
    Use == to compare values and = to assign values.
  4. 4
    Remember that > and < are exclusive, while >= and <= include equality.
  5. 5
    Use == for value equality and is for exact object identity.
  6. 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.

Related concepts

More concepts starting with C