What is the numpy.zeros() function in Python?

Overview

The numpy.zeros() function is used to return a new array of a given shape and type filled with zeros.

Syntax

The function has the following syntax:

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

Parameters

The numpy.zeros() function takes the following parameter values:

  • shape: This represents the desired or the new array.
  • dtype: This represents the desired data type of array and is optional.
  • like: This represents the array_like object or the prototype.

Return value

The numpy.zeros() function returns an array with the given shape and data type filled with zeros.

Example

import numpy as np
# creating a prototype or array_like object with an integer data type
my_array = np.zeros(5, dtype = int)
print(my_array)

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We implement the numpy.zeros() function to create an array with its 5 elements set to 0. We assigned the result to a variable my_array.
  • Line 6: We print the new array my_array.

Free Resources