0
0
NumPydata~10 mins

Array attributes (shape, dtype, ndim, size) in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the number of dimensions of the array.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.[1])
Drag options to blanks, or click blank then click option'
Andim
Bshape
Csize
Ddtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'shape' instead of 'ndim' to get the number of dimensions.
Using 'size' which gives total elements, not dimensions.
2fill in blank
medium

Complete the code to print the total number of elements in the array.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr.[1])
Drag options to blanks, or click blank then click option'
Asize
Bshape
Cndim
Ddtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'shape' which returns a tuple, not a single number.
Using 'ndim' which returns number of dimensions, not elements.
3fill in blank
medium

Complete the code to print the data type of the array elements.

NumPy
import numpy as np
arr = np.array([1, 2, 3])
print(arr.[1])
Drag options to blanks, or click blank then click option'
Ashape
Bdtype
Csize
Dndim
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'shape' or 'size' instead of 'dtype'.
Using 'ndim' which returns number of dimensions.
4fill in blank
hard

Fill both blanks to print the number of rows and columns of the array.

NumPy
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.[1][0], arr.[2][1])
Drag options to blanks, or click blank then click option'
Andim
Bshape
Csize
Ddtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ndim' which returns a single number, not indexable.
Using 'size' which is total elements, not dimensions.
5fill in blank
hard

Fill all three blanks to compute the total number of elements by multiplying the dimensions.

NumPy
import numpy as np
arr = np.zeros((2, 3, 4))
print(arr.[1][0] * arr.[2][1] * arr.[3][2])
Drag options to blanks, or click blank then click option'
Andim
Bshape
Csize
Ddtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' directly instead of computing from shape.
Trying to index 'ndim' or 'dtype'.