Skip to content
LearnPY
NumPyfunction reference

np.zeros

Create an array filled with zeros.

Signature

np.zeros(shape, dtype=float)

What it does

np.zeros allocates a new array of the requested shape and initializes every element to 0. It is useful as a placeholder before filling values or as a starting point for accumulators.

Parameters

shapeint or tuple of ints
The dimensions of the new array.
dtypedtype, optionaloptional
The element type. Defaults to float.

Returns

ndarray

Example

pythonnp.zeros example
import numpy as np

grid = np.zeros((2, 3))
print(grid)

Output

[[0. 0. 0.]
 [0. 0. 0.]]

Watch out for

  • The default dtype is float, not int; pass dtype=int if you need integer zeros.
  • A shape argument must be a tuple for multiple dimensions: (2, 3), not 2, 3.