Creating Arrays (NumPy)
NumPy offers many constructors for building arrays: np.array from existing data, np.zeros / np.ones / np.full for constant fills, np.arange / np.linspace for sequences, and np.random for random data.
NumPy
np.array(list) is the most direct: it converts a Python list (or nested lists for higher dimensions) into an ndarray, inferring the dtype. For arrays you will populate later, the constant builders zeros, ones, full, and the uninitialized empty pre-allocate the right shape cheaply.
For numeric sequences, arange counts by a fixed step and linspace splits an interval into a fixed number of points. Random data comes from a Generator made with np.random.default_rng. Identity matrices come from np.eye.
The recurring lesson is to allocate the full array first and fill it with vectorized assignments, rather than appending in a Python loop, which defeats NumPy's speed advantage.
Syntax
np.array(data) | np.zeros(shape) | np.arange(stop) | np.linspace(a, b, n)Examples
Python
import numpy as np
print(np.array([1, 2, 3]))
print(np.full((2, 2), 7))Output:
[1 2 3]
[[7 7]
[7 7]]