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

NumPy · Class

ndarray

numpy.ndarray is a multidimensional, homogeneous array whose elements have a fixed-size representation described by a dtype. Its constructor exposes shape, storage, strides, offset, and memory-order controls, but ordinary arrays should usually be created with array, zeros, or empty.

How it works

An ndarray stores a multidimensional collection whose elements share one fixed-size data type. Its shape describes dimensions, while dtype describes each element’s representation; although the class can be instantiated directly with memory-layout controls, ordinary arrays are generally created with array, zeros, or empty.

Syntax

numpy.ndarray(shape, dtype=np.float64, buffer=None, offset=0, strides=None, order=None)

Parameters

NameTypeDescription
shapetuple of intsShape of created array.
dtypedata-type, optionalAny object that can be interpreted as a NumPy data type.
bufferobject exposing buffer interface, optionalUsed to fill the array with data.
offsetint, optionalOffset of array data in buffer.
stridestuple of ints, optionalStrides of data in memory.
order{'C', 'F'}, optionalRow-major (C-style) or column-major (Fortran-style) order.

Returns

An ndarray object representing a multidimensional, homogeneous array of fixed-size items.

Key points

  • An ndarray represents data across one or more dimensions, with every element using the same fixed-size format.
  • The associated dtype records each element’s representation, including its byte order and storage size.
  • shape determines the dimensions and extents of the created array.
  • dtype accepts any object NumPy can interpret as a data type.
  • strides specify how data is spaced in memory for the array.
  • offset sets where the array’s data begins inside the supplied buffer.

Common mistakes

  • Prefer array, zeros, or empty for normal construction rather than calling ndarray directly; the documented constructor is a low-level instantiation method.
  • The buffer parameter supplies storage through an object exposing the buffer interface; it is not ordinary initial element data.
  • The order choice controls memory layout, so C and Fortran order should not be treated as interchangeable when layout matters.

Example: Create and inspect an ndarray

import numpy as np

values = np.array([[1, 2], [3, 4]], dtype=np.int64)
print(values.shape)
print(values.dtype)
print(values[0, 1])

Creates a two-dimensional homogeneous array with an explicit integer data type, then inspects its shape, data type, and an indexed element.

When to use it

Multidimensional numeric storage

Representing numeric data with multiple dimensions while keeping one consistent element type.

Buffer-backed data access

Inspecting or controlling an array’s memory representation when interoperating with buffer-backed data.

Memory-layout control

Choosing C-style or Fortran-style layout for algorithms or interoperability that depend on memory order.

Interview questions

QWhat is an ndarray?

ndarray is NumPy’s multidimensional, homogeneous array object. Homogeneous means every element has the same fixed-size representation, described by the associated dtype.

Follow-up: How would you inspect those properties? Use the array’s shape for its dimensions and dtype for the element representation.

QWhat does the ndarray constructor accept?

The constructor accepts shape, dtype, buffer, offset, strides, and order. Shape defines the array dimensions; dtype defines the element type, while the remaining arguments provide low-level storage and layout control.

Follow-up: Why avoid direct construction for routine arrays? The documentation recommends array, zeros, or empty instead.

QHow do dtype and memory-layout arguments affect an ndarray?

dtype can be any object interpretable as a NumPy data type. order selects C-style row-major or Fortran-style column-major layout, and strides describe data spacing in memory.

Follow-up: What role does offset play? It specifies the starting offset of array data within the supplied buffer.

  • numpy.array
  • numpy.zeros
  • numpy.empty
  • numpy.dtype

Source: numpy.ndarray#. Checked 2026-10-15.