Concept Flow - Array attributes (shape, dtype, ndim, size)
Create numpy array
Access .shape
Access .dtype
Access .ndim
Access .size
Done
Create a numpy array, then check its shape, data type, number of dimensions, and total size step-by-step.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.shape) print(arr.dtype) print(arr.ndim) print(arr.size)
| Step | Action | Expression Evaluated | Result |
|---|---|---|---|
| 1 | Create array | arr = np.array([[1, 2, 3], [4, 5, 6]]) | [[1 2 3] [4 5 6]] |
| 2 | Access shape | arr.shape | (2, 3) |
| 3 | Access dtype | arr.dtype | int32 |
| 4 | Access ndim | arr.ndim | 2 |
| 5 | Access size | arr.size | 6 |
| 6 | End | No more attributes to access | Execution stops |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | After Step 5 |
|---|---|---|---|---|---|---|
| arr | undefined | [[1 2 3] [4 5 6]] | [[1 2 3] [4 5 6]] | [[1 2 3] [4 5 6]] | [[1 2 3] [4 5 6]] | [[1 2 3] [4 5 6]] |
| arr.shape | undefined | undefined | (2, 3) | (2, 3) | (2, 3) | (2, 3) |
| arr.dtype | undefined | undefined | undefined | int32 | int32 | int32 |
| arr.ndim | undefined | undefined | undefined | undefined | 2 | 2 |
| arr.size | undefined | undefined | undefined | undefined | undefined | 6 |
Array attributes in numpy: - shape: tuple showing size in each dimension - dtype: data type of elements - ndim: number of dimensions - size: total number of elements Use these to understand array structure quickly.