NumPy Tutorial NumPy Statistics NumPy References

NumPy - zeros() function



The NumPy zeros() function returns a new array of specified shape and data type, filled with zeros.

Syntax

numpy.zeros(shape, dtype=float, order='C')

Parameters

shape Required. Specify shape of the returned array in form of int or tuple of ints.
dtype Optional. Specify the desired data-type for the array. Default: float
order Optional. Specify whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Two possible values are: C (C-style) and F (Fortran-style). Default: 'C'

Return Value

Returns an array (ndarray) of zeros with the given shape, dtype, and order.

Example: Create 2-D array of zeros

In the example below, zeros() function is used to create a 2 dimensional array of zeros of specified shape.

import numpy as np
Arr = np.zeros((2,3))
print(Arr)

The output of the above code will be:

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

Example: zeros() function with dtype parameter

The zeros() function can be used with dtype parameter to provide the data type of the elements of the array. In the example below, data type of the returned array is complex.

import numpy as np
Arr = np.zeros((2,2), dtype=complex)
print(Arr)

The output of the above code will be:

[[ 0.+0.j  0.+0.j]
 [ 0.+0.j  0.+0.j]]

❮ NumPy - Functions