0
0
NumPydata~20 mins

np.prod() for product in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.prod() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.prod() on a 1D array
What is the output of the following code?
import numpy as np
arr = np.array([2, 3, 4])
result = np.prod(arr)
print(result)
NumPy
import numpy as np
arr = np.array([2, 3, 4])
result = np.prod(arr)
print(result)
A24
B9
CNone
DTypeError
Attempts:
2 left
💡 Hint
np.prod() multiplies all elements in the array.
data_output
intermediate
2:00remaining
Product along axis in 2D array
Given the 2D array below, what is the output of np.prod(arr, axis=0)?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
result = np.prod(arr, axis=0)
print(result)
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
result = np.prod(arr, axis=0)
print(result)
A[4 6]
B[1 2]
C[3 4]
D[3 8]
Attempts:
2 left
💡 Hint
Multiplying elements down each column.
Predict Output
advanced
2:00remaining
Effect of dtype parameter in np.prod()
What is the output of this code?
import numpy as np
arr = np.array([1000, 2000, 3000], dtype=np.int16)
result = np.prod(arr, dtype=np.int64)
print(result)
NumPy
import numpy as np
arr = np.array([1000, 2000, 3000], dtype=np.int16)
result = np.prod(arr, dtype=np.int64)
print(result)
AOverflowError
B6000000000
C-1294967296
D6000000000.0
Attempts:
2 left
💡 Hint
dtype controls the type used for multiplication to avoid overflow.
data_output
advanced
2:00remaining
Product of an empty array
What is the output of np.prod() when applied to an empty array?
import numpy as np
arr = np.array([])
result = np.prod(arr)
print(result)
NumPy
import numpy as np
arr = np.array([])
result = np.prod(arr)
print(result)
ANone
B0.0
C1.0
DValueError
Attempts:
2 left
💡 Hint
Think about the identity value for multiplication.
visualization
expert
3:00remaining
Visualizing product along different axes
You have this 3D array:
import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

Which option correctly describes the shape of the output when applying np.prod(arr, axis=1)?
NumPy
import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
result = np.prod(arr, axis=1)
print(result)
print(result.shape)
AOutput shape is (2, 2) with values [[3, 8], [35, 48]]
BOutput shape is (2, 2, 2) same as input
COutput shape is (2, 2) with values [[4, 6], [12, 14]]
DOutput shape is (2,) with values [24, 1680]
Attempts:
2 left
💡 Hint
Multiplying elements along axis 1 means multiplying rows inside each 2D slice.