Skip to main content
v2026.11,858 entries · CC-BY 4.0

NumPy Zeros: np.zeros() Syntax and Examples

np.zeros() creates an array of zeros: shape, dtype and order explained, with tested examples and how it differs from np.empty, ones and full.

Written and maintained by CASRAI Editorial Board

Last updated

numpy.zeros returns a new array of a given shape and type, filled with zeros. In research code it is the standard way to reserve a result array before a loop fills it in. Its signature in the current NumPy documentation is numpy.zeros(shape, dtype=None, order='C', *, device=None, like=None). This guide covers the call (also searched as python numpy zeros), the parameters that change its output, and how it differs from np.empty, np.ones and np.full.

What np.zeros is used for in research

The main research use is preallocation: create an array of the final size, filled with a known value, then write each result into it. A matrix of zeros is also the neutral starting point for an accumulator (running totals, a confusion-matrix tally, a histogram you build by hand) and for a mask or design matrix you fill column by column.

The reason to prefer zeros over an uninitialised array for this work is that every cell has a defined value. A cell your code never reached still reads 0, not whatever was in memory, and that matters for computational reproducibility: the same script must give the same array on a different machine. For converting existing Python lists rather than creating blank arrays, see creating a NumPy array from a list.

Install or access

NumPy is installed from the official installation page at https://numpy.org/install/. The two commands it gives are:

  • pip install numpy
  • conda install numpy (inside a conda environment, created with conda create -n my-env and activated with conda activate my-env)

The same page recommends a virtual environment for better dependency management: python -m venv my-env, then source my-env/bin/activate on macOS and Linux or my-env\Scripts\activate on Windows, then pip install numpy. To share the environment with collaborators see sharing a reproducible conda environment. Import the package as np; print(np.__version__) confirms the installed version.

Step-by-step: create an array of zeros

  1. Import the package: import numpy as np.
  2. Choose the shape. A single integer gives a one-dimensional array: np.zeros(5). A tuple gives more dimensions: np.zeros((2, 3)) is 2 rows by 3 columns.
  3. Choose the dtype. The default is numpy.float64. For counts, pass an integer type: np.zeros((5,), dtype=np.int_).
  4. Optionally choose the memory order. order='C' (the default) stores rows contiguously (row-major); order='F' stores columns contiguously (column-major).
  5. Check what you got: a.shape and a.dtype.
  6. Fill the array in your analysis code, and record the shape and dtype in your methods note.

Parameters in the current signature:

  • shape: an int or a tuple of ints, for example (2, 3) or 2.
  • dtype: the data type, default numpy.float64.
  • order: 'C' or 'F', default 'C'.
  • device: for Array API interoperability only; it must be "cpu". Added in NumPy 2.0.0.
  • like: lets the call create an array that is not a NumPy array, through the __array_function__ protocol. Added in NumPy 1.20.0.

The function returns an ndarray.

Output and how to read it

The examples below follow the NumPy documentation. The output shown was also reproduced on NumPy 2.5.1 on 2026-10-29.

Call Output What it shows
np.zeros(5) array([0., 0., 0., 0., 0.]) Floats by default; the trailing dots mark float values
np.zeros((5,), dtype=np.int_) array([0, 0, 0, 0, 0]) Integers when you ask for them
np.zeros((2, 3)) two rows of [0., 0., 0.] A tuple sets the dimensions
np.zeros((2,), dtype=[('x','i4'),('y','i4')]) array([(0, 0), (0, 0)], dtype=[('x', '<i4'), ('y', '<i4')]) A structured dtype gives named fields, each zeroed

The documentation’s own example shows the same zeros with extra padding (array([ 0., 0., 0., 0., 0.])); current NumPy prints them without it, which is cosmetic.

np.zeros(5) versus np.zeros((5,))

Both produce the same one-dimensional array of length 5, because an int is accepted as a shape. The tuple form becomes necessary as soon as you want a second dimension.

np.zeros versus np.empty, np.ones and np.full

The NumPy quickstart describes the difference directly: “The function zeros creates an array full of zeros, the function ones creates an array full of ones, and the function empty creates an array whose initial content is random and depends on the state of the memory.” The numpy.empty documentation adds that, unlike zeros, ones and full, it “does not initialize the values of the array, and may therefore be marginally faster”, and that the stored values are arbitrary: “For reproducible behavior, be sure to set each element of the array before reading.” The practical rule for research code is to use zeros unless you will certainly write every element before reading any, and to expect unrelated-looking numbers if you print an empty array.

When you want an array with the same shape and type as one you already have, the documentation’s “See also” points to zeros_like.

Troubleshooting

  • TypeError when passing dimensions as separate arguments. Tested on NumPy 2.5.1: np.zeros(2, 3) raised TypeError: Cannot interpret '3' as a data type, because the second positional argument is dtype. Wrap the dimensions in a tuple: np.zeros((2, 3)).
  • Output shows 0. instead of 0. The default dtype is float64. Pass dtype=int (or np.int_) if you need integer counts. Tested: np.zeros((2, 3), dtype=int) returns an integer array.
  • Unexpected values in a new array. If an array you expected to be zero holds arbitrary numbers, check whether it was created with np.empty rather than np.zeros.
  • A keyword is rejected. device exists from NumPy 2.0.0 and like from 1.20.0; check np.__version__ if an older install does not accept them.

Free alternatives

These are related functions in NumPy itself, listed in the zeros documentation’s “See also” section. All ship with NumPy.

Function What it returns
numpy.zeros New array of the given shape and type, filled with zeros
numpy.zeros_like Array of zeros with the same shape and type as a given array
numpy.ones Array filled with ones
numpy.full Array filled with a value you choose
numpy.empty New array of the given shape and type without initialising entries; values are arbitrary

For evenly spaced values rather than a constant fill, see NumPy linspace; for choosing values by condition, see NumPy where.

FAQ

What does numpy zeros do?

It returns a new array of the shape and dtype you give, with every element set to zero. The default dtype is float64.

How do I make a 2D array of zeros in NumPy?

Pass the shape as a tuple: np.zeros((2, 3)) gives 2 rows and 3 columns.

How do I get integer zeros instead of floats?

Set the dtype: np.zeros((5,), dtype=np.int_) returns array([0, 0, 0, 0, 0]).

What is the difference between np.zeros and np.empty?

zeros fills the array with 0. empty does not initialise it, so its values are arbitrary and you must set each element before reading; it may be marginally faster.

What do the order and like parameters do?

order chooses row-major (‘C’) or column-major (‘F’) memory layout. like (NumPy 1.20.0 and later) lets the function create a non-NumPy array through __array_function__.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about NumPy Zeros: np.zeros() Syntax and Examples

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.