Bird
Raised Fist0
NumPydata~10 mins

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

Choose your learning style10 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'.

Practice

(1/5)
1. Which numpy attribute tells you the total number of elements in an array?
easy
A. size
B. shape
C. ndim
D. dtype

Solution

  1. Step 1: Understand the meaning of each attribute

    shape shows dimensions, dtype shows data type, ndim shows number of dimensions, and size shows total elements.
  2. Step 2: Identify the attribute for total elements

    The attribute size gives the total number of elements in the array.
  3. Final Answer:

    size -> Option A
  4. Quick Check:

    Total elements = size [OK]
Hint: Total elements? Use size attribute [OK]
Common Mistakes:
  • Confusing shape with total elements
  • Thinking ndim gives total elements
  • Mixing dtype with size
2. Which of the following is the correct way to get the number of dimensions of a numpy array arr?
easy
A. arr.size()
B. arr.ndim
C. arr.shape()
D. arr.dtype

Solution

  1. Step 1: Recall attribute vs method syntax in numpy

    Attributes like ndim and dtype are accessed without parentheses, methods like size() do not exist.
  2. Step 2: Identify correct attribute for number of dimensions

    arr.ndim gives the number of dimensions directly.
  3. Final Answer:

    arr.ndim -> Option B
  4. Quick Check:

    Number of dimensions = arr.ndim [OK]
Hint: Use attribute without () for ndim, size, dtype [OK]
Common Mistakes:
  • Using parentheses with attributes
  • Confusing shape with ndim
  • Trying to call size() as method
3. Given the code:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape, arr.dtype, arr.ndim, arr.size)

What is the output?
medium
A. (2, 3) float64 2 6
B. (3, 2) int64 2 6
C. (2, 3) int64 2 6
D. (2, 3) int32 2 6

Solution

  1. Step 1: Analyze the array shape and data type

    The array has 2 rows and 3 columns, so shape is (2, 3). The default integer type on many systems is int64.
  2. Step 2: Check number of dimensions and total size

    It is a 2D array, so ndim = 2. Total elements = 2*3 = 6.
  3. Final Answer:

    (2, 3) int64 2 6 -> Option C
  4. Quick Check:

    shape=(2,3), dtype=int64, ndim=2, size=6 [OK]
Hint: Shape = rows x columns, size = product of shape [OK]
Common Mistakes:
  • Mixing rows and columns in shape
  • Assuming dtype is int32 by default
  • Confusing ndim with size
4. What is wrong with this code snippet?
import numpy as np
arr = np.array([1, 2, 3])
print(arr.shape())
medium
A. print cannot display tuples
B. arr is not defined
C. shape returns a number, not a tuple
D. shape is an attribute, not a method; remove parentheses

Solution

  1. Step 1: Identify usage of shape

    shape is an attribute, so it should be accessed without parentheses.
  2. Step 2: Correct the code

    Change arr.shape() to arr.shape to fix the error.
  3. Final Answer:

    shape is an attribute, not a method; remove parentheses -> Option D
  4. Quick Check:

    Attributes have no () [OK]
Hint: Attributes like shape have no () [OK]
Common Mistakes:
  • Using parentheses with attributes
  • Thinking shape returns a single number
  • Assuming print can't show tuples
5. You have a numpy array arr with shape (4, 5, 6). Which attribute would you use to find the total number of elements, and what is that number?
hard
A. arr.size, 120
B. arr.ndim, 3
C. arr.shape, (4, 5, 6)
D. arr.dtype, depends on data

Solution

  1. Step 1: Understand the attributes

    size gives total elements, ndim gives number of dimensions, shape gives dimensions, dtype gives data type.
  2. Step 2: Calculate total elements from shape

    Total elements = 4 * 5 * 6 = 120, so arr.size = 120.
  3. Final Answer:

    arr.size, 120 -> Option A
  4. Quick Check:

    Total elements = product of shape = 120 [OK]
Hint: Multiply shape dimensions or use size [OK]
Common Mistakes:
  • Confusing ndim with total elements
  • Using shape instead of size
  • Ignoring that dtype is unrelated to size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard