Conditional Statements: if, elif, and else
A conditional statement needs a decision.
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
- Python evaluates the if condition first.
- If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
- If none is True, the else branch runs.
- 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.
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.
A boolean expression evaluates to True or False, and both values belong to the bool data type.
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.
- 1Identify the operands
The left operand is 5 and the right operand is 5.
- 2Identify the operator
The == operator compares the values of the two operands.
- 3Compare 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?
Reveal 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 >.
| Operator | Relationship 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 |
| is | Whether both operands refer to the exact same object |
| is not | Whether 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
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 likeUsing = when the conditional needs to ask whether two values are equal.Why it failsIn Python, = assigns a value; it does not compare two values.
Do this insteadUse == when the condition must compare values.Common mistake Assuming > includes equality
What it looks likeExpecting a comparison between equal values to make > True.
Why it failsThe > operator is exclusive and requires the left operand to be strictly larger.
Do this insteadUse >= when equality should also satisfy the comparison.
Common mistake Assuming == and is ask the same question
What it looks likeUsing 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 insteadChoose == 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.
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.
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.
- 14 < 9
Four is strictly smaller than nine, so the result is True.
- 29 <= 9
The operands are equal, and <= includes equality, so the result is True.
- 310 != 10
The operands have equal values, so they are not unequal; the result is False.
- 43 >= 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
- 1Identify the boolean expression that supplies the conditional decision.
- 2Check whether the expression produces True or False.
- 3Use == to compare values and = to assign values.
- 4Remember that > and < are exclusive, while >= and <= include equality.
- 5Use == for value equality and is for exact object identity.
- 6Before 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.