Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.zeros

Creates a new NumPy array with a specified shape and type, initializing every element to zero. The shape, dtype, memory order, implementation reference, and device can be selected.

How it works

Call np.zeros with either one integer or a tuple describing each dimension. It allocates an ndarray with that shape, fills every element with zero, and lets you choose the element type and memory layout; the optional like and device arguments can direct array creation through compatible array implementations and devices.

Syntax

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

Parameters

NameTypeDescription
shapeint or tuple of intsShape of the new array; provide an integer or a tuple of integers.
dtypedata-type, optionalElement data type. If omitted, the default is numpy.float64.
order{'C', 'F'}, optionalMemory layout for multidimensional data: C-style or Fortran-style.
likearray_like, optionalReference object whose array type determines creation behavior when supported.
devicestr, optionalDevice on which to place the array, when supported by the selected array implementation.

Returns

out — ndarray: A new array of zeros with the requested shape, dtype, order, and applicable creation options.

Key points

  • np.zeros allocates a fresh zero-filled array rather than modifying an existing array.
  • Pass an integer for a one-dimensional result or a tuple to define multiple dimensions.
  • Use dtype to control the element type, including NumPy scalar types.
  • The order argument selects C or Fortran memory organization for multidimensional output.
  • The result is an ndarray reflecting the requested shape, element type, and storage order.

Common mistakes

  • order controls memory layout, not the visible shape or values; choose C or F when layout matters.
  • Without an explicit dtype, zeros are floating-point values rather than integers.

Example: Create a typed zero-filled array

import numpy as np

values = np.zeros((2, 3), dtype=np.int32)
print(values)
print(values.dtype, values.shape)

Creates a 2-by-3 integer array whose elements are all zero.

When to use it

Initialize working storage

Preallocate a numeric buffer before filling it in iterative or vectorized code.

Build zero-valued tensors

Create model inputs, masks, counters, or parameter arrays with known dimensions.

Control representation

Request integer or smaller-width elements to match an API or reduce storage.

Use alternate array implementations

Create arrays compatible with another array implementation through like.

Interview questions

QWhat does np.zeros return, and how is its shape specified?

np.zeros creates and returns a new ndarray with the requested shape, dtype, and order, with every element initialized to zero. The shape may be a single integer or a tuple of dimensions.

Follow-up: How would you ensure compact integer storage? Pass an integer dtype such as np.int32 or np.int8 explicitly.

QWhat dtype does np.zeros use by default?

The default dtype is numpy.float64. Passing dtype=np.int32, for example, produces integer zeros instead of floating-point zeros.

Follow-up: What changes if the dtype is structured? The returned array uses that structured dtype, so each zero-filled record contains fields initialized according to the dtype.

QWhat is the purpose of the order argument?

order selects C-style row-major or Fortran-style column-major storage for multidimensional arrays. It affects memory organization and can matter for interoperability or performance.

Follow-up: When should order be specified? Specify it when a downstream operation or external library expects a particular contiguous layout; otherwise the default C order is usually sufficient.

  • numpy.ones
  • numpy.empty
  • numpy.full
  • numpy.zeros_like

Source: numpy.zeros. Checked 2026-10-15.