Handle a boundary value
Quick review (2 minutes)
Imagine a courier whose price depends on a parcel's weight.
In an if, elif, else chain Python works out the conditions in order and runs the block of the first one that is true. No later branch is tested or run. If every condition is false, the else block runs, and if there is no else, no block of the chain runs.
<= and >= include the value they are compared with. < and > leave it out.
List the values where the rule changes. In the parcel rule they are 2 and 10.
For each one, read the words of the rule and decide which side the value itself belongs to. The rule says up to 2, so 2 stays with the lighter parcels.
Use
iffor the first band,eliffor each middle band andelsefor what is left. Put the conditions in order from one end of the range. The parcel program starts from the lowest cutoff.Pick test values: each boundary value and the whole number on the other side of the change. The rule keeps 2 in the lower band, so the other side starts at 3. It keeps 10 in the middle band, so the other side starts at 11. The test values are 2, 3, 10 and 11.
Work out the expected price for each test value by hand, from the words of the rule.
Run or trace the program with each test value and compare. A mismatch at a boundary points to
<against<=or to the order of the conditions.
Writing < where the rule needs <= moves one value to the wrong side. With weight < 2, a parcel with a weight of 2 costs 9 coins. Every other whole-number weight is still priced correctly, so the mistake shows only in a test with 2.
Using a second if where the rule needs elif is the other common slip. Two separate if statements are two separate decisions, and Python tests each one it reaches. With if weight <= 2: followed by if weight <= 10:, a parcel with a weight of 2 passes both, and the second block replaces the price of 4 with 9. An elif is tested only when every condition above it in the same chain was false.
1 In plain words
Imagine a courier whose price depends on a parcel's weight. The rule is invented for this lesson, and the parcel program below follows it. A weight of 2 or less costs 4 coins. A weight above 2 and up to 10 costs 9 coins. Any heavier parcel costs 15 coins. Most weights sit well inside one price band. A weight of exactly 2 or exactly 10 sits where one price ends and the next begins. Python picks one of the three outcomes by asking conditions in order.
A value where a rule changes is a boundary value. < and <= give different answers only when the two sides are equal, so a wrong choice between them shows only at the boundary value.
A rule with three outcomes can be written with three branches. Python adds a branch with elif, which is short for "else if". An elif line comes after the if block, lined up with if, and carries its own condition and colon. An if statement can have several elif lines, and the else part stays optional.
Python asks the conditions from the top down and runs the block of the first one that is true. It skips every branch after that one, even if a later condition would also be true. If none is true, the else block runs, when there is one.
Remember one sentence: test each boundary value and the whole number on the other side of it. That advice is this lesson's own, and the lesson keeps to whole-number weights. To look up if, elif and else, see section 4.1 of the Python 3.13 tutorial.
2 Picture it
A table of four test weights. A weight of 2 passes the test weight <= 2 and costs 4. A weight of 3 first passes weight <= 10 and costs 9. A weight of 10 also first passes weight <= 10 and costs 9. A weight of 11 passes neither test and costs 15. The rows for the boundary values 2 and 10 are marked.
3 Why
Because the first true condition wins, each later condition is asked only about values the earlier ones turned away. In the parcel program, weight <= 10 is reached only when weight <= 2 was false. So on that line it means "more than 2 and at most 10" without saying the first half.
That shorter condition depends on the order. Put weight <= 10 first and every parcel with a weight of 2 or less passes it and is priced at 9 coins. Python reports no error, because both orders are legal programs. A test with a weight of 2 shows the wrong price.
The words of the rule choose the operator. "Up to 2" includes 2, so the test is <=. "Under 2" would leave 2 out, and the test would be <.
4 The rule
In an if, elif, else chain Python works out the conditions in order and runs the block of the first one that is true.
No later branch is tested or run. If every condition is false, the else block runs, and if there is no else, no block of the chain runs.
<= and >= include the value they are compared with. < and > leave it out.
5 How
- List the values where the rule changes. In the parcel rule they are 2 and 10.
- For each one, read the words of the rule and decide which side the value itself belongs to. The rule says up to 2, so 2 stays with the lighter parcels.
- Use
iffor the first band,eliffor each middle band andelsefor what is left. Put the conditions in order from one end of the range. The parcel program starts from the lowest cutoff. - Pick test values: each boundary value and the whole number on the other side of the change. The rule keeps 2 in the lower band, so the other side starts at 3. It keeps 10 in the middle band, so the other side starts at 11. The test values are 2, 3, 10 and 11.
- Work out the expected price for each test value by hand, from the words of the rule.
- Run or trace the program with each test value and compare. A mismatch at a boundary points to
<against<=or to the order of the conditions.
6 Worked example
Type 2, the first boundary value
reply = input()
weight = int(reply)
if weight <= 2:
price = 4
elif weight <= 10:
price = 9
else:
price = 15
print(price)
Standard input
2
Standard output
4
Save the code as parcel.py, run it and type 2. The first condition, 2 <= 2, is True, so the price is 4 and Python skips the other two branches.
Type 10, the second boundary value
reply = input()
weight = int(reply)
if weight <= 2:
price = 4
elif weight <= 10:
price = 9
else:
price = 15
print(price)
Standard input
10
Standard output
9
Run the file again and type 10. 10 <= 2 is False, so Python asks the elif condition. 10 <= 10 is True and the price is 9.
Type 11, the other side of the second boundary
reply = input()
weight = int(reply)
if weight <= 2:
price = 4
elif weight <= 10:
price = 9
else:
price = 15
print(price)
Standard input
11
Standard output
15
Run the file once more and type 11. Both conditions are False, so the else block sets the price to 15.
Trace
| weight | weight <= 2 | weight <= 10 | price | Explanation |
|---|---|---|---|---|
| 2 | True | not asked | 4 | The first condition is True, so Python never asks the second. |
| 3 | False | True | 9 | The other side of the first boundary. The elif branch runs. |
| 10 | False | True | 9 | The second boundary. <= keeps 10 in the middle band. |
| 11 | False | False | 15 | The other side of the second boundary. No condition is True, so else runs. |
7 The trap
Writing < where the rule needs <= moves one value to the wrong side. With weight < 2, a parcel with a weight of 2 costs 9 coins. Every other whole-number weight is still priced correctly, so the mistake shows only in a test with 2.
Using a second if where the rule needs elif is the other common slip. Two separate if statements are two separate decisions, and Python tests each one it reaches. With if weight <= 2: followed by if weight <= 10:, a parcel with a weight of 2 passes both, and the second block replaces the price of 4 with 9. An elif is tested only when every condition above it in the same chain was false.
Quick checks
Quick check 1 of 2
In a grading rule invented for this question, a score of 80 or more earns a merit, and a score of 50 or more earns a pass. What does this program display? Then name the two boundary values of the rule and, for each, the whole number on the other side of the change.
score = 50
if score >= 80:
print("merit")
elif score >= 50:
print("pass")
else:
print("retake")Show the steps and the answer
It displays pass. The boundary values are 50 and 80. The whole numbers on the other side are 49 and 79.
Why: 50 >= 80 is False and 50 >= 50 is True, so the elif block runs. The rule changes at 50 and at 80. "50 or more" keeps 50 with the passes, so the other side of that change is 49. "80 or more" keeps 80 with the merits, so the other side is 79.
Quick check 2 of 2
A sign invented for this question says "Children under 5 ride free." A program uses the condition age <= 5 for the free ride. Which single age shows the mistake, and what should the condition be?
Show the steps and the answer
Age 5 shows the mistake. The condition should be age < 5.
Why: "Under 5" leaves 5 out, but <= includes it, so a five-year-old rides free when the sign says to pay. age <= 5 and age < 5 agree for every other whole-number age.
Source: Python Software Foundation, Python 3.13 Library Reference, Built-in Types: Comparisons
Sources
- Python 3.13 Language Reference, 8 Compound statements and 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Library Reference, Built-in Types: Comparisons. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Tutorial, 4.1 if Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.