Three fill loops build constant-value float buffers of length 4: all zeros, all ones, and all sevens. The trace shows the same append pattern repeated three times, with each list growing one element per iteration.

By hand

Run three separate loops, each appending a constant to a fresh list. The c variable holds the fill value for the third loop, matching how np.full takes an explicit constant argument.

naive.py
Replay: real traced execution (multi-file project)
n = 4
c = 7.0
zeros = []
for i in range(n):
    zeros.append(0.0)
ones = []
for i in range(n):
    ones.append(1.0)
full = []
for i in range(n):
    full.append(c)
print('RESULT:', (zeros, ones, full))
  1. n ← 4

    1n = 42c = 7.0
    values this step4n
  2. c ← 7.0

    1n = 42c = 7.03zeros = []
    values this step7.0c
  3. zeros ← []

    2c = 7.03zeros = []4for i in range(n):
    values this step[]zeros
  4. i ← 0, zeros ← [0.0]

    pass 1 of 4
    3zeros = []4for i in range(n):5    zeros.append(0.0)6ones = []
    values this step0i[] [0.0]zeros
    All 4 passes — pass 1 is the card above
    passizeros
    10[] [0.0]
    20 1[0.0] [0.0, 0.0]
    31 2[0.0, 0.0] [0.0, 0.0, 0.0]
    42 3[0.0, 0.0, 0.0] [0.0, 0.0, 0.0, 0.0]
  5. for i in range(n):

    3zeros = []4for i in range(n):5    zeros.append(0.0)
  6. ones ← []

    5    zeros.append(0.0)6ones = []7for i in range(n):
    values this step[]ones
  7. i ← 0, ones ← [1.0]

    pass 1 of 4
    6ones = []7for i in range(n):8    ones.append(1.0)9full = []
    values this step3 0i[] [1.0]ones
    All 4 passes — pass 1 is the card above
    passiones
    13 0[] [1.0]
    20 1[1.0] [1.0, 1.0]
    31 2[1.0, 1.0] [1.0, 1.0, 1.0]
    42 3[1.0, 1.0, 1.0] [1.0, 1.0, 1.0, 1.0]
  8. for i in range(n):

    6ones = []7for i in range(n):8    ones.append(1.0)
  9. full ← []

    8    ones.append(1.0)9full = []10for i in range(n):
    values this step[]full
  10. i ← 0, full ← [7.0]

    pass 1 of 4
    9full = []10for i in range(n):11    full.append(c)12print('RESULT:', (zeros, ones, full))
    values this step3 0i[] [7.0]full
    All 4 passes — pass 1 is the card above
    passifull
    13 0[] [7.0]
    20 1[7.0] [7.0, 7.0]
    31 2[7.0, 7.0] [7.0, 7.0, 7.0]
    42 3[7.0, 7.0, 7.0] [7.0, 7.0, 7.0, 7.0]
  11. for i in range(n):

    9full = []10for i in range(n):11    full.append(c)
  12. stdout ← RESULT: ([0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0], [7.0, 7.0, 7.0, 7.0])

    11    full.append(c)12print('RESULT:', (zeros, ones, full))
    values this stepRESULT: ([0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0], [7.0, 7.0, 7.0, 7.0])stdout

With NumPy

np.zeros(n), np.ones(n), and np.full(n, c) each allocate a typed buffer and fill it in one call without an explicit loop. The snapshot shows each array's shape, element type, and values in a labeled block.

library.py
import numpy as np

n = 4
c = 7.0
zeros = np.zeros(n)
ones = np.ones(n)
full = np.full(n, c)
for name, arr in [('zeros', zeros), ('ones', ones), ('full', full)]:
    print(f'{name}: shape: {arr.shape} dtype: {arr.dtype} values: {arr.tolist()}')
print('RESULT:', (zeros.tolist(), ones.tolist(), full.tolist()))
zeros: shape: (4,) dtype: float64 values: [0.0, 0.0, 0.0, 0.0]
ones: shape: (4,) dtype: float64 values: [1.0, 1.0, 1.0, 1.0]
full: shape: (4,) dtype: float64 values: [7.0, 7.0, 7.0, 7.0]
RESULT: ([0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0], [7.0, 7.0, 7.0, 7.0])

Implementation notes

  • All three functions default to dtype=float64. Pass dtype=int (or another dtype) to change the element type.
  • np.zeros is the conventional way to pre-allocate an output buffer before filling it with computed values — a common pattern in the reduction and broadcasting lessons that follow.
  • np.empty(n) is faster than np.zeros(n) because it skips the initialisation step, but the contents are undefined. Use it only when every element will be overwritten before it is read.
  • np.zeros_like(arr) and np.ones_like(arr) create a zero/one array with the same shape and dtype as an existing array — useful when the shape is not known until runtime.
  • Shape, dtype, and values are shown explicitly here because ndarray.__repr__ output varies with NumPy version and print options.