Challenge - 5 Problems
np.full() Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.full() with integer fill value
What is the output of this code?
import numpy as np arr = np.full((2, 3), 7) print(arr)
NumPy
import numpy as np arr = np.full((2, 3), 7) print(arr)
Attempts:
2 left
💡 Hint
Look at the shape argument (2, 3) and the fill value 7.
✗ Incorrect
np.full creates an array of the given shape filled with the fill value. Here, shape (2,3) means 2 rows and 3 columns filled with 7.
❓ data_output
intermediate2:00remaining
Shape and dtype of np.full() array
What is the shape and data type of the array created by this code?
import numpy as np arr = np.full((4,), 3.14, dtype=np.float32) print(arr.shape, arr.dtype)
NumPy
import numpy as np arr = np.full((4,), 3.14, dtype=np.float32) print(arr.shape, arr.dtype)
Attempts:
2 left
💡 Hint
Check the shape tuple and the dtype argument.
✗ Incorrect
The shape is (4,) meaning a 1D array with 4 elements. The dtype is float32 as specified.
🔧 Debug
advanced2:00remaining
Identify the error in np.full() usage
What error does this code raise?
import numpy as np arr = np.full(3, [1, 2]) print(arr)
NumPy
import numpy as np arr = np.full(3, [1, 2]) print(arr)
Attempts:
2 left
💡 Hint
np.full expects a scalar fill value, but here a list is given.
✗ Incorrect
np.full expects a scalar fill value. Passing a list causes a broadcasting error because it cannot fit the list into each scalar element.
🚀 Application
advanced2:00remaining
Creating a masked array with np.full()
You want to create a 3x3 numpy array filled with -1 to mark missing data. Which code correctly creates this array with integer type?
Attempts:
2 left
💡 Hint
Check the shape format and dtype for integer type.
✗ Incorrect
Option B uses a tuple for shape, integer fill value, and dtype=int which is correct. Option B uses string fill value, C uses list for shape which is allowed but less common, D uses float dtype which is not integer.
🧠 Conceptual
expert3:00remaining
Memory behavior of np.full() with mutable fill value
What happens if you run this code?
import numpy as np arr = np.full((2,), [0]) arr[0].append(1) print(arr)
NumPy
import numpy as np arr = np.full((2,), [0]) arr[0].append(1) print(arr)
Attempts:
2 left
💡 Hint
Think about how np.full handles mutable objects as fill_value.
✗ Incorrect
np.full uses the same object reference for each element if fill_value is mutable. So modifying one element modifies all references.