Array Attributes: shape, ndim, size

Reviewed & published by Brayan K

Array attributes are the read-only properties every NumPy ndarray exposes — like shape, ndim, and size — that describe its structure, dimensions, and element count without changing any of its data.

Part of the free NumPy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll learn to read each attribute, see how they relate (size is always the product of shape), and use them to understand any array at a glance.

What You'll Learn in This Lesson

1 .shape — Size of Each Dimension

The .shape attribute returns a tuple with one number per dimension. For a 2D array it reads as (rows, columns). A 1D array of length 4 has shape (4,) — note the trailing comma that marks a single-element tuple.

import numpy as np

v = np.array([10, 20, 30, 40])
m = np.array([[1, 2, 3],
              [4, 5, 6]])

print(v.shape)   # (4,)     -> 1D, four elements
print(m.shape)   # (2, 3)   -> 2 rows, 3 columns

# ✅ Expected output:
# (4,)
# (2, 3)

Expected output: (4,) for the vector and (2, 3) for the matrix — one entry per dimension.

2 .ndim — Number of Dimensions

The .ndim attribute gives the number of dimensions (axes). It always equals the length of the .shape tuple: a vector is 1, a matrix is 2, and a stack of matrices is 3.

import numpy as np

a3 = np.array([[[1, 2], [3, 4]],
               [[5, 6], [7, 8]]])

print(a3.shape)  # (2, 2, 2)
print(a3.ndim)   # 3
print(len(a3.shape) == a3.ndim)  # True

# ✅ Expected output:
# (2, 2, 2)
# 3
# True

Expected output: shape (2, 2, 2), 3 dimensions, and True — ndim matches the length of shape.

3 .size — Total Element Count

The .size attribute is the total number of elements in the whole array. It always equals the product of every number in .shape. A (2, 3) array has 2 * 3 = 6 elements.

import numpy as np

m = np.array([[1, 2, 3],
              [4, 5, 6]])

print(m.shape)   # (2, 3)
print(m.size)    # 6   -> 2 * 3

# size always equals the product of the shape
rows, cols = m.shape
print(rows * cols == m.size)  # True

# ✅ Expected output:
# (2, 3)
# 6
# True

Expected output: shape (2, 3), size 6, and True — size is exactly rows times columns.

4 dtype, itemsize & nbytes

Three more attributes round out the picture: .dtype is the element type, .itemsize is the bytes per element, and .nbytes is the total data size, which equals .size * .itemsize.

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]], dtype=np.float64)

print(arr.dtype)     # float64
print(arr.itemsize)  # 8   -> bytes per element
print(arr.size)      # 6   -> total elements
print(arr.nbytes)    # 48  -> 6 * 8

# ✅ Expected output:
# float64
# 8
# 6
# 48

Expected output: float64, 8, 6, and 48 bytes — exactly size times itemsize.

🎯 YOUR TURN: Fill in the Blanks

Replace each ___ so the program reports a 2D array's shape, dimensions, and element count.

import numpy as np

grid = np.array([[1, 2, 3, 4],
                 [5, 6, 7, 8]])

print(grid.___)    # fill in the attribute for the shape -> (2, 4)

print(grid.___)    # fill in the attribute for number of dimensions -> 2

print(grid.___)    # fill in the attribute for total elements -> 8

Expected output: (2, 4), 2, then 8. (Answers: shape, ndim, size.)

⚠️ Common Errors & Quick Reference

❌ TypeError: 'tuple' object is not callable

You wrote arr.shape() with parentheses. shape is an attribute, not a method.

✅ Fix: drop the parentheses — use arr.shape.

❌ Confusing .size with len()

len(arr) returns only the first dimension, while .size counts every element.

✅ Fix: use arr.size for the total element count.

AttributeMeaning
arr.shapeTuple of dimension sizes, e.g. (2, 3)
arr.ndimNumber of dimensions
arr.sizeTotal number of elements
arr.nbytesTotal bytes = size × itemsize

🏆 Mini Challenge: Full Array Report

Build a 3×4 array of ones and print a complete report of its shape, ndim, size, and bytes.

import numpy as np

a = np.ones((3, 4))

print("Shape:", a.shape)     # Shape: (3, 4)
print("Ndim:", a.ndim)       # Ndim: 2
print("Size:", a.size)       # Size: 12
print("Bytes:", a.nbytes)    # Bytes: 96  -> 12 * 8

# ✅ Expected output:
# Shape: (3, 4)
# Ndim: 2
# Size: 12
# Bytes: 96

❓ Frequently Asked Questions

Lesson complete — you can read any array!

You now know how to read shape, ndim, size, dtype, itemsize, and nbytes, and you understand that size is always the product of the numbers in shape.

Practice quiz

What does .shape return for a 2D array with 2 rows and 3 columns?

  • 6
  • (3, 2)
  • (2, 3)
  • 2

Answer: (2, 3). .shape is a tuple of (rows, columns), so (2, 3).

What is the .shape of np.array([10, 20, 30, 40])?

  • (4,)
  • 4
  • (1, 4)
  • (4, 1)

Answer: (4,). A 1D array of length 4 has shape (4,) with a trailing comma.

What does .ndim return for a 3D array of shape (2, 2, 2)?

  • 2
  • 3
  • 8
  • (2, 2, 2)

Answer: 3. .ndim is the number of dimensions, which is 3.

For a (2, 3) array, what does .size return?

  • 2
  • 3
  • (2, 3)
  • 6

Answer: 6. .size is the total element count, equal to 2 * 3 = 6.

Which statement is always true?

  • arr.size == len(arr.shape)
  • arr.ndim == arr.size
  • arr.ndim == len(arr.shape)
  • arr.shape == arr.size

Answer: arr.ndim == len(arr.shape). .ndim always equals the length of the .shape tuple.

For a float64 array, what does .itemsize return?

  • 8
  • 4
  • 64
  • 1

Answer: 8. float64 uses 8 bytes per element, so .itemsize is 8.

What is .nbytes for a (2, 3) float64 array?

  • 6
  • 48
  • 8
  • 16

Answer: 48. .nbytes equals .size * .itemsize = 6 * 8 = 48.

How do you correctly read an array's shape?

  • arr.shape()
  • shape(arr)
  • arr.size
  • arr.shape

Answer: arr.shape. shape is an attribute, not a method — no parentheses.

How does .size relate to .shape?

  • It is the largest value in shape
  • It is the first value in shape
  • It is the product of all values in shape
  • It is the length of shape

Answer: It is the product of all values in shape. .size always equals the product of the numbers in .shape.

What does .dtype describe?

  • The data type of the elements
  • The number of dimensions
  • The total bytes used
  • The number of rows

Answer: The data type of the elements. .dtype reports the element data type, e.g. float64 or int64.

Continue this course

Frequently asked questions

What does .shape return in NumPy?

The .shape attribute returns a tuple giving the size of the array along each dimension. For a 2D array with 2 rows and 3 columns, .shape is (2, 3). For a 1D array of length 4 it is (4,).

What is the difference between .ndim and .size?

.ndim is the number of dimensions (axes) of the array — 1 for a vector, 2 for a matrix. .size is the total number of elements in the whole array, which equals the product of all the numbers in .shape.

How is .size related to .shape?

.size always equals the product of the values in .shape. A (2, 3) array has size 2 * 3 = 6, and a (2, 2, 2) array has size 2 * 2 * 2 = 8.

What do .itemsize and .nbytes tell me?

.itemsize is the number of bytes one element takes (8 for float64), and .nbytes is the total memory the array's data uses, equal to .size times .itemsize.