Python:NumPy size

SrikartikMateti's avatar
Published Oct 29, 2025
Contribute to Docs

NumPy’s size attribute is used to find the total number of elements in an array.

  • 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.size

Parameters:

size doesn’t take any parameters because it’s an attribute, not a method.

Return value:

Returns an integer representing the total number of elements in the array.

Example 1: Getting the Size of an Array Using size

In this example, the code prints the total number of elements in the array:

import numpy as np
np_array = np.array([[1, 2, 3], [4, 5, 6]])
print(np_array.size)

The output of this code is:

6

The array has 2 rows × 3 columns = 6 elements in total.

Example 2: Comparing .shape and .size

In this example, shape displays the array’s dimensions, while size shows the total number of elements in the array:

import numpy as np
arr = np.array([[10, 20, 30], [40, 50, 60]])
print("Shape:", arr.shape)
print("Size:", arr.size)

The output of this code is:

Shape: (2, 3)
Size: 6

shape returns the dimensions of the array, while size gives the total number of elements.

Codebyte Example: Using .size in a NumPy Operation

In this example, size returns the total number of elements (12) in a reshaped 3×4 NumPy array:

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