0
0
NumPydata~10 mins

np.full() for custom-filled arrays in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.full() for custom-filled arrays
Start
Call np.full(shape, fill_value)
Create empty array of given shape
Fill every element with fill_value
Return filled array
End
np.full() creates an array of a given shape and fills every element with the value you choose.
Execution Sample
NumPy
import numpy as np
arr = np.full((3, 2), 7)
print(arr)
This code creates a 3x2 array where every element is 7.
Execution Table
StepActionShapeFill ValueArray State
1Call np.full(3, 2)7Array not created yet
2Create empty array of shape (3, 2)(3, 2)7[[?, ?], [?, ?], [?, ?]] (uninitialized)
3Fill all elements with 7(3, 2)7[[7, 7], [7, 7], [7, 7]]
4Return filled array(3, 2)7[[7, 7], [7, 7], [7, 7]]
💡 Array is fully filled with 7, np.full returns the array.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
arrNone[[?, ?], [?, ?], [?, ?]][[7, 7], [7, 7], [7, 7]][[7, 7], [7, 7], [7, 7]]
Key Moments - 3 Insights
Why does the array show '?' or uninitialized values before filling?
At Step 2, the array memory is allocated but not yet filled, so it contains whatever was in memory (uninitialized). Only at Step 3 does np.full fill all elements with the fill value.
Can np.full create arrays of any shape?
Yes, np.full accepts any shape tuple (like (3, 2), (4,), (2, 2, 2)) and creates arrays of that shape filled with the value, as shown in the Shape column.
What happens if the fill value is a different data type?
np.full will try to cast the fill value to the array's data type. If not specified, it infers from the fill value. This means the array elements will all be the same type.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the array state after Step 2?
ANone
B[[7, 7], [7, 7], [7, 7]]
C[[?, ?], [?, ?], [?, ?]]
DEmpty list []
💡 Hint
Check the 'Array State' column for Step 2 in the execution_table.
At which step does the array get filled with the value 7?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Array State' columns in the execution_table.
If we change the fill value to 0, how would the final array look?
A[[?, ?], [?, ?], [?, ?]]
B[[0, 0], [0, 0], [0, 0]]
C[[7, 7], [7, 7], [7, 7]]
DAn error occurs
💡 Hint
Refer to the final 'Array State' in variable_tracker and replace 7 with 0.
Concept Snapshot
np.full(shape, fill_value) creates a new array of the given shape.
Every element in the array is set to fill_value.
Shape is a tuple like (rows, columns).
Useful to initialize arrays with a constant value.
Returns a NumPy ndarray filled with the value.
Full Transcript
np.full() is a NumPy function that creates an array with a shape you choose and fills every element with the value you provide. First, it allocates memory for the array with the given shape. Initially, this memory is uninitialized and may contain random values. Then, np.full fills every element with the fill value. Finally, it returns the filled array. For example, np.full((3, 2), 7) creates a 3-row, 2-column array where every element is 7. This is useful when you want an array pre-filled with a specific number instead of zeros or random values.