mass = 3.54
if mass > 3.0:
print(mass, 'is large')
mass = 2.07
if mass > 3.0:
print(mass, 'is large')3.54 is large
if, elif, and else statements to branch program execution.and, or, and not.if statements control whether a block runsAn if statement evaluates a condition and executes its indented body only if that condition is True. The structure mirrors a for loop: the header ends with a colon and the body is indented:
3.54 is large
On its own, an if with a fixed value is not very interesting. Conditionals become useful inside loops, where the condition is evaluated for each item in the collection:
else and elif handle the remaining caseselse specifies what to do when the if condition is not met. elif (short for “else if”) inserts an additional condition between if and else. Python tests each condition in order and executes the first branch whose condition is True, then skips the rest:
3.54 is large
2.07 is small
9.22 is HUGE
1.86 is small
1.71 is small
The order of branches matters. Python stops at the first true condition, so putting a less-restrictive condition first swallows values that should have matched a later, more-restrictive one:
grade is C
Arrange conditions from most to least restrictive to avoid this mistake.
Python tests each condition once when execution reaches it and does not re-evaluate after variables change. This is different from how a spreadsheet works:
adjusting velocity
Even though velocity is now 50.0, the if branch is not re-visited. Conditionals inside loops can, however, produce evolving behaviour by re-testing the condition on each iteration:
0 : 10.0
1 : 20.0
2 : 30.0
3 : 25.0
4 : 20.0
final velocity: 30.0
A variable-trace table is a useful tool for following this kind of loop:
| i | velocity (start of iteration) | velocity (end of iteration) |
|---|---|---|
| 0 | 10.0 | 20.0 |
| 1 | 20.0 | 30.0 |
| 2 | 30.0 | 25.0 |
| 3 | 25.0 | 20.0 |
| 4 | 20.0 | 30.0 |
Use and, or, and not to build compound conditions. When mixing and and or in the same expression, always add parentheses — the precedence rules are easy to misread:
Without parentheses, a or b and c means a or (b and c), which may not be what you intended.
What does this program print?
25.0
25.0
The first condition (pressure > 50.0) is true when pressure is 71.9, so that branch executes and elif is skipped.
Fill in the blanks so that this program replaces negative values with 0 and positive values (including zero) with 1:
[0, 1, 1, 1, 0, 1]
Modify this program so that it finds the largest and smallest values in the list regardless of the range of values:
What are the advantages and disadvantages of using None as the initial sentinel value?
-54 100
Using None as a sentinel means the code works correctly regardless of the range of values — you do not need to guess a safe initial minimum or maximum. The disadvantage is that you need the special-case first-iteration check; forgetting it causes a TypeError when min(None, v) is called.
if statement executes its body only when the condition is True.elif adds additional conditions; else handles everything not caught above.and, or, and not to combine conditions; parentheses clarify intent.