Challenge - 5 Problems
Scalar Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of scalar multiplication on a NumPy array
What is the output of this code?
import numpy as np arr = np.array([1, 2, 3]) result = arr * 4 print(result)
NumPy
import numpy as np arr = np.array([1, 2, 3]) result = arr * 4 print(result)
Attempts:
2 left
💡 Hint
Multiplying an array by a number multiplies each element by that number.
✗ Incorrect
Each element in the array is multiplied by 4, so [1*4, 2*4, 3*4] = [4, 8, 12].
❓ data_output
intermediate2:00remaining
Result of adding a scalar to a 2D NumPy array
What is the resulting array after adding 10 to each element?
import numpy as np arr = np.array([[1, 2], [3, 4]]) result = arr + 10 print(result)
NumPy
import numpy as np arr = np.array([[1, 2], [3, 4]]) result = arr + 10 print(result)
Attempts:
2 left
💡 Hint
Adding a scalar adds it to every element in the array.
✗ Incorrect
Adding 10 to each element results in [[1+10, 2+10], [3+10, 4+10]] = [[11, 12], [13, 14]].
🔧 Debug
advanced2:00remaining
Identify the error in scalar division of a NumPy array
What error does this code produce?
import numpy as np arr = np.array([10, 20, 30]) result = arr / 0 print(result)
NumPy
import numpy as np arr = np.array([10, 20, 30]) result = arr / 0 print(result)
Attempts:
2 left
💡 Hint
Dividing by zero in NumPy arrays triggers a warning, not an exception.
✗ Incorrect
NumPy issues a RuntimeWarning and returns 'inf' or 'nan' values instead of raising an error.
🧠 Conceptual
advanced2:00remaining
Effect of scalar subtraction on a NumPy array
If you subtract 5 from each element of the array
np.array([7, 8, 9]), what is the resulting array?Attempts:
2 left
💡 Hint
Subtracting a scalar subtracts it from every element.
✗ Incorrect
Subtracting 5 from each element gives [7-5, 8-5, 9-5] = [2, 3, 4].
🚀 Application
expert2:00remaining
Calculate the mean after scalar multiplication
Given the array
np.array([2, 4, 6, 8]), what is the mean of the array after multiplying each element by 3?NumPy
import numpy as np arr = np.array([2, 4, 6, 8]) result = arr * 3 mean_val = result.mean() print(mean_val)
Attempts:
2 left
💡 Hint
Multiply first, then find the average of the new array.
✗ Incorrect
Multiplying by 3 gives [6, 12, 18, 24]. The mean is (6+12+18+24)/4 = 60/4 = 15.0.