Python:NumPy .zeros()
Anonymous contributor
Published Jun 23, 2025
Contribute to Docs
.zeros() is a NumPy function used to create a new array of a specified shape, filled entirely with zeros. It is commonly used to initialize arrays before assigning them meaningful data.
Syntax
numpy.zeros(shape, dtype=float, order='C')
Parameters:
shape: An integer or tuple of integers indicating the dimensions of the array. For example,(3, 2)creates a 2D array with 3 rows and 2 columns.dtype: The desired data type for the array elements (default isfloat).order: Whether to store the array in row-major ('C', default) or column-major ('F') order.
Return value:
Returns a new array of the given shape, filled entirely with zeros.
Example
This example creates a 1D array of five zeros and a 2D array of zeros with shape (3, 2) and integer data type using NumPy’s .zeros() function:
import numpy as nparray1 = np.zeros(5)print(array1)array2 = np.zeros((3, 2), dtype=int)print(array2)
The output of this code will be:
[0. 0. 0. 0. 0.][[0 0][0 0][0 0]]
Codebyte Example: Initializing a Sensor Readings Matrix
In this codebyte example, .zeros() is used to initialize a 2D array to store temperature sensor readings for 7 days across 3 different locations:
Here, .zeros() prepares a placeholder matrix to store daily sensor readings, which will be updated later as data is collected.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours