1.2.19 · D1Introduction to Programming (Python)

Foundations — `range()` — start, stop, step

2,013 words9 min readBack to topic

Before we can read a single line like range(2, 9, 2), we must earn every mark on the page. Below, each symbol is given plain words, a picture, and the reason the topic cannot live without it. They are ordered so each one leans only on the ones above it.


1. The counting numbers and the number line

Picture a ruler that stretches forever in both directions. Every tick is one integer. Zero sits in the middle; positives go right, negatives go left.

Figure — `range()` — start, stop, step
Question: Can range() ever output ?
No — it deals only in integers (whole ticks).

2. A step: moving by a fixed amount

Look at the arrows below. Every arrow is exactly the same length — that "same length every time" is what evenly spaced means, and it is the whole personality of a range.

Figure — `range()` — start, stop, step

This connects to Arithmetic sequences: a list where each term differs from the last by a fixed amount is called an arithmetic sequence, and a range is precisely a discrete arithmetic sequence made of integers.

Question: What does a negative step do on the number line?
Hops leftward, so the numbers decrease.

3. The -th term formula — reading

Now we can decode the parent note's central formula. Let us earn each symbol.

Let's read it on a picture. Take start = 2, step = 2:

Figure — `range()` — start, stop, step
  • — zero hops, still standing on the start.
  • — one hop.
  • — two hops.
Question: In , what does the subscript mean?
The position of the term (0 = first, 1 = second, ...), not multiplication.

4. The half-open interval — "stop OUT"

Picture a fence with a solid dot on the left end (you stand there) and a hollow dot on the right end (a wall you must not touch).

Figure — `range()` — start, stop, step
Question: What does the round bracket in tell you?
9 is excluded — you stop before it.

5. The stopping test: , , and why the sign of the step flips it

The parent note says: keep producing terms while they are still valid. "Valid" depends on which way you walk:

Step direction Keep going while Picture
step > 0 (rightward) approaching wall from the left
step < 0 (leftward) approaching floor from the right
Question: With a negative step, what comparison decides whether a term is included?
(greater than).

6. Ceiling and — reading the count formula

The parent's count formula uses two new symbols.

Question: Why is needed in the count formula?
To turn a negative (wrong-direction) result into 0 — you can't have negative elements.

7. "Lazy" and "immutable" — two words the parent leans on

Question: What does "lazy" mean for a range object?
It generates each number on demand instead of storing them all at once.

Prerequisite map

Integers and the number line

Step as a fixed hop

Sign of step = direction

Term formula a_k = start + k times step

Half-open interval start included stop excluded

Comparison test less-than or greater-than

Ceiling and max for the count

Lazy and immutable sequence

range start stop step


Equipment checklist

Self-test: can you answer each before opening the parent note?

Is an integer?
No — integers are whole numbers only.
What does a step of do on the number line?
Hops 3 ticks to the left (numbers decrease).
In , what is ?
The start itself (zero hops taken).
Is the subscript in multiplication?
No — it labels the term's position.
Does include 8?
No — the round bracket excludes it.
Which test is used when the step is positive?
.
Which test is used when the step is negative?
.
What is ?
4 (round up).
What is ?
0 (the larger value).
Why does range use so little memory?
It is lazy — it computes numbers on demand instead of storing them.

Connections

  • Parent topic — this page builds every symbol it assumes.
  • Arithmetic sequences — the term formula is a discrete arithmetic progression.
  • Indexing and slicing — same half-open convention and 0-based counting.
  • for loops — where these integers get consumed one at a time.
  • Lists and list() — the eager cousin that materialises every number.
  • numpy.arange / linspace — the float-capable relatives (recall: range is integers only).
  • enumerate() — pairs an index with a value when you need both.