1.2.19Introduction to Programming (Python)

`range()` — start, stop, step

1,696 words8 min readdifficulty · medium

WHAT it is

Deriving the count formula from scratch — WHY this form?

  1. Terms are start, start+step, start+2·step, ….
  2. We want the count of valid terms before crossing stop.
  3. The "distance" to cover is stop - start; each step covers step. Dividing gives the number of steps that fit: (stop - start) / step.
  4. Because stop is excluded, we round up (ceil): even a tiny leftover distance still needs one more term to reach/pass the boundary... but we stop before it, so the count is exactly that ceiling.
  5. If the division is negative (e.g. going the wrong direction), no terms fit → max(0, …).

A handy integer-only version (no ceil): with step > 0, n=stopstart+step1step(integer floor division)n = \frac{\text{stop} - \text{start} + \text{step} - 1}{\text{step}} \quad(\text{integer floor division})


HOW to use it

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

Common mistakes


Recall Feynman: explain to a 12-year-old

Imagine a hopscotch grid. start is the square you jump on first. step is how far each hop is. stop is a wall you must not land on — you stop the moment your next hop would hit or pass the wall. So range(2, 9, 2) means: start on 2, hop 2 squares each time (2→4→6→8), and stop before the wall at 9. Python doesn't draw the whole hopscotch at once; it just tells you the next square whenever you ask. That's why it's fast even for a million squares.


Active recall

What are the three argument forms of range()?
range(stop), range(start, stop), range(start, stop, step).
In range(start, stop, step), is stop included?
No — it is excluded (half-open [start, stop)).
Default start and step in range(stop)?
start = 0, step = 1.
Formula for the k-th term of a range?
ak=start+kstepa_k = \text{start} + k\cdot\text{step}.
Formula for number of elements?
n=max(0,(stopstart)/step)n = \max(0, \lceil(\text{stop}-\text{start})/\text{step}\rceil).
Output of list(range(10, 0, -2))?
[10, 8, 6, 4, 2].
What does range(5, 5) give?
[] (empty).
What happens with step = 0?
ValueError — step must be non-zero.
Why does range(10, 0) give []?
Default step is +1, can't go up from 10 to 0.
How do you make a range that includes N?
range(start, N+1).
Can range take float arguments?
No — TypeError; integers only.
Is a range object a list?
No — it's a lazy immutable sequence (generates on demand).

Connections

  • for loopsrange() is the most common loop driver.
  • Lists and list() — wrap a range to materialise it: list(range(...)).
  • Indexing and slicing — both use the same half-open [start, stop) convention.
  • Arithmetic sequencesa_k = start + k·step is a discrete arithmetic progression.
  • numpy.arange / linspace — the float-capable cousins of range().
  • enumerate() — alternative when you need index and value.

Concept Map

behaves as

produces

has forms

has forms

obeys

stop excluded, so

derives

elements follow

powers

saves memory in

range start stop step

Lazy counting machine

Immutable integer sequence

Half-open interval start,stop

Term a_k = start + k·step

Count n = max 0, ceil stop-start /step

for-loop iteration

range stop → start 0 step 1

range start,stop,step full control

Hinglish (regional understanding)

Intuition Hinglish mein samjho

range() ko samajhne ka sabse easy tarika: ye ek counting machine hai jo numbers ek-ek karke deta hai. Tum bas batao start (kahan se shuru), stop (kahan rukna hai), aur step (har baar kitna jump). Sabse important baat — stop wala number kabhi included nahi hota. Jaise range(2, 9, 2) matlab 2 se shuru, 2-2 ka jump: 2, 4, 6, 8 — 9 nahi aata kyunki wo wall hai jise touch nahi karna.

Formula khud derive karo, ratna mat: k-th number hai ak=start+kstepa_k = \text{start} + k \cdot \text{step}, aur jab tak step positive hai tab tak a_k < stop chalta raho. Total kitne numbers? (stopstart)/step\lceil (\text{stop}-\text{start})/\text{step} \rceil. Isiliye len(range(n)) exactly n hota hai — koi off-by-one tension nahi.

Common galti: students sochte hain range(1, 10) mein 10 aayega — nahi aata! Agar N tak chahiye to range(1, N+1) likho. Aur agar ulta count karna hai (10 se 0 tak) to negative step do: range(10, 0, -2), warna empty list milti hai kyunki default step +1 hai.

Ye chhota sa topic 80/20 wala hai — har for loop mein iska use hoga, to ye solid hona chahiye. Mnemonic yaad rakho: "Start IN, Stop OUT, Step JUMP." Bas itna pakka karlo, baaki sab easy.

Go deeper — visual, from zero

Test yourself — Introduction to Programming (Python)

Connections