Generating Arrays (arange, linspace, zeros, ones)
NumPy gives you built-in functions to quickly create arrays — without manually typing the values.
These are useful for building test data or initializing arrays.
np.arange(start, stop, step)
Creates evenly spaced values from start to stop (excluding stop).
np.arange(0, 10, 2) # [0 2 4 6 8]
np.linspace(start, stop, num)
Creates a specific number of evenly spaced values including the stop value.
np.linspace(0, 1, 5) # [0. 0.25 0.5 0.75 1.0]
np.zeros(shape)
and np.ones(shape)
Create arrays filled with all 0s or all 1s.
Pass in a shape like (3,)
or (2, 3)
.
np.zeros((2, 2)) # [[0. 0.]
# [0. 0.]]
Summary
arange
: spaced by step (likerange()
)linspace
: spaced by number of pointszeros
/ones
: fill arrays with fixed values
What’s Next
You’ll now try generating different types of arrays using these tools in Jupyter.