0
0
NumPydata~10 mins

Array attributes (shape, dtype, ndim, size) in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
NumPy
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)
Create a 2D numpy array and print its shape, data type, number of dimensions, and total number of elements.
Execution Table
StepActionExpression EvaluatedResult
1Create arrayarr = np.array([[1, 2, 3], [4, 5, 6]])[[1 2 3] [4 5 6]]
2Access shapearr.shape(2, 3)
3Access dtypearr.dtypeint32
4Access ndimarr.ndim2
5Access sizearr.size6
6EndNo more attributes to accessExecution stops
💡 All four attributes accessed; execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
arrundefined[[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.shapeundefinedundefined(2, 3)(2, 3)(2, 3)(2, 3)
arr.dtypeundefinedundefinedundefinedint32int32int32
arr.ndimundefinedundefinedundefinedundefined22
arr.sizeundefinedundefinedundefinedundefinedundefined6
Key Moments - 3 Insights
Why does arr.shape return a tuple like (2, 3)?
Because arr has 2 rows and 3 columns, shape shows the size in each dimension as a tuple. See execution_table step 2.
What does arr.dtype tell us about the array?
It tells the type of data stored in the array elements, here int32 means 32-bit integers. See execution_table step 3.
Why is arr.ndim equal to 2?
Because the array has two dimensions: rows and columns. See execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of arr.size at step 5?
A2
B6
C(2, 3)
Dint64
💡 Hint
Check the 'Result' column at step 5 in the execution_table.
At which step does arr.dtype get evaluated?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for 'Access dtype' in the 'Action' column of execution_table.
If the array was 3D, which attribute would show the number of dimensions?
Aarr.ndim
Barr.shape
Carr.size
Darr.dtype
💡 Hint
Check the meaning of arr.ndim in key_moments and execution_table step 4.
Concept Snapshot
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.
Full Transcript
We create a numpy array with two rows and three columns. Then we check its shape, which is (2, 3), meaning 2 rows and 3 columns. Next, we look at dtype, which tells us the data type of the elements, here int32. Then ndim shows the number of dimensions, which is 2 for this 2D array. Finally, size gives the total number of elements, which is 6. This helps us understand the array's structure and contents clearly.