Open In App

Create a Numpy array filled with all zeros - Python

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to create a Numpy array filled with all zeros, given the shape and type of array. We can use Numpy.zeros() method to do this task.

Let's understand with the help of an example:

Python
import numpy as np

# Create a 1D array of zeros with 5 elements
array_1d = np.zeros(5)
print(array_1d)

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

Explanation:

  • np.zeros(5) function creates a 1D array with 5 elements, all initialized to 0.
  • By default, the data type of the array is float, so the zeros are represented as 0.0.

Syntax of Creating a NumPy Array filled with all zeros

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

Parameters

  • shape: Tuple or int specifying the dimensions of the array (e.g., (rows, columns)).
  • dtype (optional): Data type of the array (default is float).
  • order (optional): Memory layout of the array. 'C' (row-major) or 'F' (column-major).

Creating a 2D Zero Array

Python
import numpy as np

# Create a 2D array of zeros (3 rows, 4 columns)
array_2d = np.zeros((3, 4))
print(array_2d)

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

Explnation:

  • np.zeros((3, 4)) function creates a 2D array with 3 rows and 4 columns.
  • The shape of the array is specified as a tuple (3, 4).

Specifying Data Type

Python
import numpy as np

# Create an integer zero array
array_int = np.zeros((2, 3), dtype=int)
print(array_int)

Output
[[0 0 0]
 [0 0 0]]

Explanation:

  • Here, the dtype=int argument is used to specify that the array elements should be integers.
  • The resulting array is 2x3 (2 rows, 3 columns) with all elements as 0, but they are now integers instead of floats.

Using Column-Major Order

Python
import numpy as np

# Create an array with column-major order
array_column_major = np.zeros((2, 3), order='F')
print(array_column_major)

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

Explanation:

  • By default, NumPy arrays are stored in row-major order (C order). However, specifying order='F' changes this to column-major order (Fortran-style).
  • In column-major order, elements in the same column are stored contiguously in memory.

Next Article
Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy