zeros() and ones() (NumPy)
np.zeros() and np.ones() create new arrays of a given shape filled entirely with 0s or 1s. They are the usual way to pre-allocate an array you will fill in later, or to build constant arrays.
NumPy
Pass a shape tuple and you get an array of that size: np.zeros((2, 3)) is a 2-by-3 grid of zeros. Both default to the float64 dtype, so add dtype=int if you want integer fills. The related np.full(shape, value) fills with any constant, and np.empty allocates uninitialized memory (faster but full of garbage values).
Pre-allocating with zeros and then assigning into the array is the standard pattern when you must build results in a loop, because growing a NumPy array element by element is expensive. The 'like' variants — zeros_like(a), ones_like(a) — copy another array's shape and dtype.
np.eye and np.identity are the specialized cousins that build identity matrices for linear algebra.
Syntax
np.zeros(shape, dtype=float) | np.ones(shape, dtype=float)Examples
Python
import numpy as np
print(np.zeros((2, 3)))Output:
[[0. 0. 0.]
[0. 0. 0.]]