← Back to NumPy Course | Chapter 3: Array Attributes | Lesson 1 of 6

shape

The shape tells you how many rows, columns and so on the array has.

In this page:

  1. shape
Syntax
python
arr.shape    # (rows, cols)

shape

The shape attribute is a tuple giving the length of each dimension. A 2-by-3 matrix has shape (2, 3). Assigning to shape or using reshape changes the layout without altering the data.

Note: A 1-D array of length 5 has shape (5,) with a trailing comma, because it is a tuple.

Example: shape

python
import numpy as np

m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.shape)
print("rows:", m.shape[0], "cols:", m.shape[1])
print(np.array([1, 2, 3]).shape)

# Output:
# (2, 3)
# rows: 2 cols: 3
# (3,)
Related Topics
Common Mistakes
  1. Forgetting shape is a tuple, not a number
  2. Confusing (5,) with (5, 1)
  3. Treating shape as a method and calling it
Chapter Summary
  • shape is a tuple of dimension sizes
  • It is an attribute so no parentheses
  • 1-D shapes look like (n,)
  • Product of shape equals size
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.