Skip to course content
Free course

Python Foundations / Module 8 / Shape, Dimensions, Size, and Dtype

Module 8 lesson

Shape, Dimensions, Size, and Dtype

Unit ID: M08-U02 Estimated active time: 20-30 minutes

For a two-dimensional array, shape is (rows, columns). ndim counts axes, size counts all values, and dtype names the stored type.

print(study_minutes.shape)  # (2, 4)
print(study_minutes.ndim)   # 2
print(study_minutes.size)   # 8
print(study_minutes.dtype)  # int64

Shape is part of the data contract. A calculation can be valid Python and still answer the wrong question if rows and columns are reversed.

Before calculating, write a sentence such as: "Rows are learners and columns are weeks." Then assert the expected column count.

assert study_minutes.ndim == 2
assert study_minutes.shape[1] == 4

Practice: for shape (6, 4), state the number of learners, weeks, dimensions, and stored values.