A stepped numeric sequence built manually with a while-loop, then generated in one call. The trace steps through x advancing by step each iteration and seq growing until x reaches stop.

By hand

Initialise x = start, then loop while x < stop: append x to seq, then advance x by step. This is exactly the rule np.arange follows — stop is exclusive, and the sequence ends at the last value strictly below it.

naive.py
start = 1.0
stop = 4.0
step = 0.5
seq = []
x = start
while x < stop:
    seq.append(x)
    x += step
print('RESULT:', seq)

With NumPy

np.arange(start, stop, step) generates the same sequence in one call. The snapshot shows the array's shape, dtype, and values, then adds a linspace(1, 4, 7) values line to contrast: linspace includes the stop value and accepts a point count rather than a step size.

library.py
import numpy as np

start, stop, step = 1.0, 4.0, 0.5
arr = np.arange(start, stop, step)
ls = np.linspace(1, 4, 7)
print('shape:', arr.shape)
print('dtype:', arr.dtype)
print('values:', [round(v, 10) for v in arr.tolist()])
print('linspace(1, 4, 7) values:', ls.tolist())
print('RESULT:', [round(v, 10) for v in arr.tolist()])
shape: (6,)
dtype: float64
values: [1.0, 1.5, 2.0, 2.5, 3.0, 3.5]
linspace(1, 4, 7) values: [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0]
RESULT: [1.0, 1.5, 2.0, 2.5, 3.0, 3.5]

Implementation notes

  • np.arange follows the same start-inclusive, stop-exclusive rule as Python's range. With a float step, floating-point rounding can occasionally produce an extra element or drop the last one — np.linspace avoids this by computing positions as start + i * (stop - start) / (n - 1).
  • Choose arange when the step size is the natural parameter (sensor sampling interval, grid resolution). Choose linspace when the number of points is the natural parameter (plotting 100 points over an interval).
  • The while-loop in the "By hand" half is the same accumulation pattern as running-total in python-data-basics/ch02, but accumulates positions rather than a cumulative sum.
  • Shape, dtype, and values are shown explicitly here because ndarray.__repr__ output varies with NumPy version and print options.