Bird
Raised Fist0
NumPydata~10 mins

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

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

Practice

(1/5)
1. What does the np.full() function do in NumPy?
easy
A. Creates an empty array with no values
B. Creates an array with random values
C. Creates an array with increasing numbers
D. Creates an array filled with the same specified value

Solution

  1. Step 1: Understand the purpose of np.full()

    The function np.full() is designed to create arrays where every element has the same value.
  2. Step 2: Compare with other array creation functions

    Unlike functions that create random or sequential arrays, np.full() fills the array with a constant value.
  3. Final Answer:

    Creates an array filled with the same specified value -> Option D
  4. Quick Check:

    np.full() = constant value array [OK]
Hint: Remember: full means fill with one value [OK]
Common Mistakes:
  • Confusing np.full() with np.random functions
  • Thinking it creates empty or zero arrays
  • Assuming it creates sequences of numbers
2. Which of the following is the correct syntax to create a 3x3 NumPy array filled with the value 7 using np.full()?
easy
A. np.full((3, 3), 7)
B. np.full(3, 3, 7)
C. np.full([3, 3], value=7)
D. np.full(7, shape=(3,3))

Solution

  1. Step 1: Identify the correct parameter order for np.full()

    np.full() takes the shape as the first argument (tuple), then the fill value as the second argument.
  2. Step 2: Check each option's syntax

    np.full((3, 3), 7) uses np.full((3, 3), 7) which matches the correct order and types. Others misuse argument order or syntax.
  3. Final Answer:

    np.full((3, 3), 7) -> Option A
  4. Quick Check:

    Shape tuple first, fill value second [OK]
Hint: Shape is first tuple, value is second argument [OK]
Common Mistakes:
  • Passing shape and value as separate arguments without tuple
  • Using keyword arguments incorrectly
  • Swapping the order of shape and fill value
3. What is the output of the following code?
import numpy as np
arr = np.full((2, 2), 5)
print(arr)
medium
A. [[5 5] [5 5]]
B. [[2 2] [2 2]]
C. [[0 0] [0 0]]
D. [[5 5 5] [5 5 5]]

Solution

  1. Step 1: Understand np.full() parameters

    The shape is (2, 2), so the array will have 2 rows and 2 columns. The fill value is 5.
  2. Step 2: Predict the printed array

    The array will be a 2x2 matrix with all elements equal to 5, printed as [[5 5]\n [5 5]].
  3. Final Answer:

    [[5 5] [5 5]] -> Option A
  4. Quick Check:

    Shape (2,2) with 5 fills = [[5 5]\n [5 5]] [OK]
Hint: Shape defines size, fill value repeats [OK]
Common Mistakes:
  • Confusing shape dimensions
  • Expecting zeros instead of fill value
  • Misreading the printed array shape
4. Identify the error in this code snippet:
import numpy as np
arr = np.full(3, 4, 2)
print(arr)
medium
A. Fill value must be a string
B. Shape argument must be a tuple
C. The third positional argument `2` is an invalid dtype specifier
D. np.full() cannot create 1D arrays

Solution

  1. Step 1: Check np.full() argument count and order

    np.full(shape, fill_value, dtype=None, ...). The call np.full(3, 4, 2) sets shape=3, fill_value=4, dtype=2, but 2 is not a valid dtype (raises ValueError: data type "2" not understood).
  2. Step 2: Confirm correct usage

    Correct syntax is np.full(3, 4) for 1D array of 4s, or specify valid dtype like dtype=float. Extra or invalid dtype causes error.
  3. Final Answer:

    The third positional argument `2` is an invalid dtype specifier -> Option C
  4. Quick Check:

    np.full(3,4,2) -> ValueError: data type "2" [OK]
Hint: Optional dtype must be valid (e.g., float, int) [OK]
Common Mistakes:
  • Passing multiple positional arguments instead of tuple shape
  • Using wrong data types for shape
  • Assuming np.full() needs three arguments
5. You want to create a 4x4 NumPy array filled with the value 9, but only the diagonal elements should be 0. Which code correctly achieves this using np.full() and other NumPy functions?
hard
A. arr = np.zeros((4,4)); arr[:] = 9; print(arr)
B. arr = np.full((4,4), 9); np.fill_diagonal(arr, 0); print(arr)
C. arr = np.full((4,4), 0); np.fill_diagonal(arr, 9); print(arr)
D. arr = np.full(4, 9); np.fill_diagonal(arr, 0); print(arr)

Solution

  1. Step 1: Create a 4x4 array filled with 9

    Using np.full((4,4), 9) creates a 4x4 array where every element is 9.
  2. Step 2: Replace diagonal elements with 0

    np.fill_diagonal(arr, 0) sets the diagonal elements of the array to 0.
  3. Step 3: Verify the output

    Printing arr shows a 4x4 array with 9 everywhere except 0 on the diagonal.
  4. Final Answer:

    arr = np.full((4,4), 9); np.fill_diagonal(arr, 0); print(arr) -> Option B
  5. Quick Check:

    Fill then fill_diagonal for custom values [OK]
Hint: Fill full array first, then change diagonal [OK]
Common Mistakes:
  • Filling zeros first then trying to fill diagonal with 9
  • Using wrong shape or 1D array for 2D problem
  • Misusing np.fill_diagonal on 1D arrays
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