Python:NumPy .nbytes

Anonymous contributor's avatar
Anonymous contributor
Published Oct 29, 2025
Contribute to Docs

The .nbytes attribute returns the total number of bytes consumed by the elements of a NumPy array. This value is calculated as the product of the number of elements in the array (given by .size) and the number of bytes per element (given by .itemsize).

  • 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

Syntax

ndarray.nbytes

Parameters:

The .nbytes attribute takes no parameters.

Return value:

Returns an integer representing the total number of bytes consumed by the array elements.

Example

The following example creates a one-dimensional NumPy array arr with 12 elements. The .nbytes attribute reports the total bytes used by all array elements. On a 64-bit system where the default integer type (int64) uses 8 bytes per element, $12 \text{ elements} \times 8 \text{ bytes}/\text{element} = 96 \text{ bytes}$:

# Import NumPy
import numpy as np
# Create a NumPy array with 12 elements (default type is usually int64, or 8 bytes per item)
arr = np.arange(12)
# Use the '.nbytes' attribute
total_bytes_nbytes = arr.nbytes
print(f"Array: {arr}")
print(f"Bytes per element (.itemsize): {arr.itemsize}")
print(f"Total number of elements (.size): {arr.size}")
print(f"Total bytes consumed (.nbytes): {total_bytes_nbytes}")

The result will be similar to the following (the value of arr.itemsize might vary based on system architecture):

Array: [ 0 1 2 3 4 5 6 7 8 9 10 11]
Bytes per element (.itemsize): 8
Total number of elements (.size): 12
Total bytes consumed (.nbytes): 96

Codebyte Example

The example below demonstrates a two-dimensional NumPy array arr with a specified data type (float32). Since float32 uses 4 bytes per element and the array contains $2 \times 3 = 6$ elements, the total memory consumed is $6 \times 4 = 24$ bytes:

Code
Output
Loading...

All contributors

Contribute to 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