Sub topic 3
Start, Stop, and Step: Exact Iteration Counts
This chapter assumes you can read Python for and while loops, know how range works, and are comfortable with the ceiling and floor functions. The previous chapter counted a plain range(n) loop. Real loops often start somewhere other than zero, skip values, or count downward. You can still find the exact iteration count from three numbers: the start, the stop, and the step.
The Half-Open Formula
Python's range(a, b, s) produces values starting at a, adding s each time, and stopping before it reaches b. This is a half-open interval: it includes a and excludes b. With a positive step and b > a, the gap between start and stop is b - a. Each iteration uses up s of that gap, and a partial step at the end still counts as one iteration. So the count is:
ceil((b - a) / s)whens > 0andb > a0whens > 0andb <= a
import math
a, b, s = 3, 20, 4
print(list(range(a, b, s))) # [3, 7, 11, 15, 19]
print(len(range(a, b, s))) # 5
print(math.ceil((b - a) / s)) # 5The gap is 17. Four full steps of 4 cover 16, and the leftover 1 still allows the value 19, so there are 5 iterations.
Counting Down
For a negative step, the loop moves from a down toward b. Swap the roles of the two ends and use the size of the step: the count is ceil((a - b) / |s|) when a > b, and 0 otherwise. For range(10, 0, -3), the values are 10, 7, 4, 1, and ceil(10 / 3) = 4.
Inclusive Bounds in While Loops
Many loops, especially while loops and C-style for loops, use <= instead of <. Then both ends are included. The count becomes:
floor((b - a) / s) + 1whens > 0andb >= a
The + 1 counts the starting value. The floor counts the full steps that still land at or below b.
a, b, s = 2, 14, 3
i, steps = a, 0
while i <= b:
steps += 1
i += s
print(steps) # 5 (values 2, 5, 8, 11, 14)
print((b - a) // s + 1) # 5Here gap means b - a for an upward loop and a - b for a downward one.
Summary Table
| Loop form | Condition | Iterations |
|---|---|---|
| `range(n)` | `n >= 0` | `n` |
| `range(a, b)` | `b >= a` | `b - a` |
| `range(a, b, s)`, `s > 0` | `b > a` | `ceil((b - a) / s)` |
| `range(a, b, s)`, `s < 0` | `a > b` | `ceil((a - b) / -s)` |
| `i = a; while i <= b: i += s` | `b >= a` | `floor((b - a) / s) + 1` |
Checking the Formula in Code
Integer arithmetic avoids floating-point rounding problems: ceil(x / s) for positive integers equals (x + s - 1) // s. The function below implements the half-open formula and compares it with len(range(...)) across many inputs.
def iterations(a, b, s):
if s > 0:
gap, step = b - a, s
else:
gap, step = a - b, -s
return max(0, (gap + step - 1) // step)
for a in range(-10, 11):
for b in range(-10, 11):
for s in (-4, -3, -1, 1, 2, 5):
assert iterations(a, b, s) == len(range(a, b, s))
print("formula matches range for all cases")Common Errors
- Off by one at the stop. Forgetting that
rangeexcludesb, or forgetting the+ 1for an inclusive<=loop. - Dividing without rounding.
(b - a) / sis a fraction when the step does not divide the gap evenly. You need the ceiling, not plain division or the floor. - Negative gaps. If the start is already past the stop, the loop runs zero times. A formula can give a negative number here, so clamp it to 0.
- Mixing directions. A positive step with
a > bruns zero times. It does not run backward.
Exercise
How many times does the body of for i in range(5, 3 * n, 2) run when n = 10? Give a general formula for n >= 2.
Show answer
For n = 10 the range is range(5, 30, 2): the values 5, 7, ..., 29, which is ceil(25 / 2) = 13 iterations. In general the count is ceil((3n - 5) / 2), which is positive for n >= 2.
Exercise
The loop i = 1; while i <= 50: i += 7 runs how many times?
Show answer
floor((50 - 1) / 7) + 1 = 7 + 1 = 8. The values are 1, 8, 15, 22, 29, 36, 43, 50.