Numpy Zeros np.zeros() in Python


Numpy zeros np.zeros() function in python is used to get an array of given shape and type filled with zeros. You can pass three parameters inside function np.zeros shape, dtype and order. Numpy zeros function returns an array of the given shape.

Basic Syntax

np.zeros(shape,dtype,order)

And the parameters are:

Parameter Description
shape like (2,3) or 2. For (2,3) it will return an array of shape 2 by 3 and for 2 it will return an array of shape 1 by 2.
dtype (Optional) This is the desired data type for an array for example numpy.int8 or numpy.float64 which is the default.
order (Optional) The values should be ‘C’ or ‘F’. This parameter describes storing data in row-major (‘C’) or column-major (‘F’) order style in memory.

Return Value

This function will return ndarray of zeros with given shape, dtype and order.

Example

The following are the examples for the np.zeros() function in Python.

Example 1

import numpy as np 
  
first = np.zeros(2, dtype = int) 
print("Matrix first : \n", first) 
  
second = np.zeros([2, 2], dtype = int) 
print("\nMatrix second : \n", second) 

Matrix first:
[0,0]
Matrix second:
[[0,0]
[0,0]]

Example 2

# Python Program for np.zeros() function 
  
import numpy as np
   
first = np.zeros((2,2), dtype=[('x', 'int'), ('y', 'float')]) 
print(first) 

The output should be:

[(0,0.0) (0,0.0)]