np.arange()
Generate evenly spaced values with a fixed step using np.arange — how it compares with Python's range, negative and float steps, and the float-step pitfall.
Watch the lesson
1 video on CodeWithMunnaX
A T20 innings has overs 1 to 20. Match numbers run 1 to 14. Writing these by hand is boring — np.arange generates them for you, exactly like Python's range() but returning a NumPy array and also allowing decimal steps.
Definition
np.arange([start,] stop[, step])returns a 1D array of evenly spaced values in the half-open interval[start, stop)— starting atstart, increasing bystepeach time, and stopping beforestop.
- Half-open interval —
startis included,stopis excluded (same rule asrangeand slicing). - step — the gap between neighbouring values; can be negative (count down) or a float (e.g. 0.25).
- Evenly spaced — every gap equals
step. You control the gap; NumPy works out how many values fit.
Syntax
python
np.arange(stop)
np.arange(start, stop)
np.arange(start, stop, step, dtype=None)| Parameter | Meaning | Default |
|---|---|---|
start | First value (included) | 0 |
stop | End value (excluded) | required |
step | Gap between values; may be negative or a float | 1 |
dtype | Output data type | inferred: int if all args are ints, otherwise float |
Example — the basic forms
python
import numpy as np
print(np.arange(6))
print(np.arange(1, 21))
print(np.arange(0, 21, 5))
print(np.arange(20, 0, -4))
print(np.arange(0, 1, 0.25))
print(np.arange(5).dtype, np.arange(5.0).dtype, np.arange(0, 5, 0.5).dtype)
print(np.arange(5, dtype=float))
print(np.arange(5, 1))text
[0 1 2 3 4 5]
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
[ 0 5 10 15 20]
[20 16 12 8 4]
[0. 0.25 0.5 0.75]
int64 float64 float64
[0. 1. 2. 3. 4.]
[]How it works:
- One argument =
stop: values 0 … 5 (6 excluded). np.arange(1, 21)= overs 1 to 20 — you write21because stop is excluded.np.arange(0, 21, 5)includes 20 because 20 < 21.- A negative step counts down: 20, 16, … 4 (0 is excluded).
- Float step 0.25 gives floats, and
1is excluded. - If any argument is a float, the result is
float64. np.arange(5, 1)is empty: with a positive step you can never get from 5 down to 1.
Example — cricket ranges
python
import numpy as np
overs = np.arange(1, 21)
print(overs)
powerplay = np.arange(1, 7)
death = np.arange(16, 21)
print("Powerplay overs:", powerplay)
print("Death overs: ", death)
print("Every 5th over: ", np.arange(5, 21, 5))text
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
Powerplay overs: [1 2 3 4 5 6]
Death overs: [16 17 18 19 20]
Every 5th over: [ 5 10 15 20]range() vs np.arange()
python
import numpy as np
print(list(range(1, 6)), np.arange(1, 6))
try:
range(0, 1, 0.25)
except TypeError as e:
print("TypeError:", e)text
[1, 2, 3, 4, 5] [1 2 3 4 5]
TypeError: 'float' object cannot be interpreted as an integerrange() | np.arange() | |
|---|---|---|
| Returns | A lazy range object (values made on demand) | A real ndarray stored in memory |
| Float start/stop/step | ❌ TypeError | ✅ allowed |
Vectorized maths (* 2) | ❌ | ✅ |
| Typical use | for loops | Creating numeric data to compute on |
| Stop value | Excluded | Excluded |
The float-step pitfall
With float steps, the number of elements is not always what you expect, because decimals like 0.1 cannot be stored exactly in binary:
python
import numpy as np
print(np.arange(1, 2, 0.1))
print(len(np.arange(1, 2, 0.1)))
print(np.arange(1, 1.3, 0.1))
print(len(np.arange(1, 1.3, 0.1)))text
[1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]
10
[1. 1.1 1.2 1.3]
4How it works: the first call behaves as expected (10 values, 2.0 excluded). But in the second, 1.3 — the stop value — appears in the output, even though stop should be excluded! NumPy computes the length as ceil((stop - start) / step), and (1.3 - 1) / 0.1 comes out as 3.0000000000000004 in floating point, which rounds up to 4.
Tip: When you want a fixed number of float values (or need the end point exactly), use
np.linspaceinstead ofnp.arangewith a float step. Usearangefor integers.
Common mistakes
- ❌
np.arange(1, 20)for overs 1–20. ✅np.arange(1, 21)— stop is excluded. - ❌ Relying on the exact length of
np.arange(a, b, 0.1). ✅ Usenp.linspace(a, b, num)for floats. - ❌
np.arange(10, 1)expecting a countdown. ✅ Give a negative step:np.arange(10, 1, -1). - ❌ Confusing
arangewith "arrange". ✅ It is a-range ("array range").
Interview tip
Q: When should you prefer np.linspace over np.arange?
A: When working with floats, or when you need a specific number of points or the end point included. arange fixes the step and its length can be off by one due to floating-point rounding; linspace fixes the count and includes the end point exactly.
Key points to remember
- np.arange(start, stop, step) returns evenly spaced values in [start, stop) — stop is excluded.
- With one argument, np.arange(n) gives 0 … n-1; start defaults to 0 and step to 1.
- Negative steps count down; float steps produce float arrays.
- Unlike range(), np.arange returns a real array and accepts floats.
- Float steps can give an unexpected length (np.arange(1, 1.3, 0.1) includes 1.3) because of rounding.
- Use arange for integer sequences; use linspace when you need a fixed count of float values.
Try it yourself
Overs, powerplay and death overs
range vs np.arange
The float-step pitfall
Practice: ball numbers
Revision Flashcards
Tap a card to flip it and see the answer.
