Array Attributes: shape, ndim, size

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.

Learn Array Attributes: shape, ndim, size in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

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.

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.

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

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.

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

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.

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

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 .

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

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

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

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

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

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

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

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

Lesson 5 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.

🚀 Up next: Indexing and Slicing — pull out exactly the elements, rows, and columns you need.

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.