All courses › Intermediate Programming › linspace and arange

linspace and arange

np.linspace(a, b, n) gives nn evenly spaced points from aa up to and including bb — both endpoints are included. np.arange(a, b, s) works like range, but returns an array and also accepts decimal steps.

linspace(a,b,n)\text{linspace}(a, b, n)spacing between points: (b−a)/(n−1)(b-a)/(n-1)

Symbols

a, ba,\ bstart and end value
nnnumber of points

Example

np.linspace(0, 1, 5) gives [0, 0.25, 0.5, 0.75, 1], spacing 1/4=0.251/4 = 0.25.

linspace takes the number of points, arange takes the step size — easy to mix up.
Practise numerics with NumPy for free →

← NumPy array and vectorization · Matrix operations →

Part of Intermediate Programming: Numerics with NumPy.