NumPy zeros()

The zeros() method creates a new array of given shape and type, filled with zeros.

Example

import numpy as np

# create an array of 5 elements filled with 0s array1 = np.zeros(5)
print(array1) # Output: [0. 0. 0. 0. 0.]

zeros() Syntax

The syntax of zeros() is:

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

zeros() Arguments

The zeros() method takes three arguments:

  • shape - desired shape of the new array (can be int or tuple of int)
  • dtype (optional) - datatype of the new array
  • order (optional) - specifies the order in which the zeros are filled

zeros() Return Value

The zeros() method returns the array of given shape, order, and datatype filled with 0s.


Example 1: Create Array With zeros

import numpy as np

# create a float array of 1s array1 = np.zeros(5
) print('Float Array: ',array1)
# create an int array of 1s array2 = np.zeros(5, dtype = int)
print('Int Array: ',array2)

Output

Float Array:  [0. 0. 0. 0. 0.]
Int Array:  [0 0 0 0 0]

If unspecified, the default dtype is float.


Example 2: Create nd-Array With zeros

import numpy as np

# create a n-d array of 1s array1 = np.zeros([2,3])
print('n-d array:\n',array1)

Output

n-d array:
 [[0. 0. 0.]
 [0. 0. 0.]]