Elementwise Math
Scalar Multiply
Every element of a 6-element list scaled by constant k = 3 via a loop.
The trace shows v taking each value and result growing one scaled element
per iteration.
By hand
Iterate over values directly (no index needed). Multiply each element v
by k and append to result. This is the map-transform pattern from
python-data-basics/ch02 with a multiply formula.
naive.py
Replay: real traced execution (multi-file project)
values = [1, 2, 3, 4, 5, 6]
k = 3
result = []
for v in values:
result.append(v * k)
print('RESULT:', result)
values ← [1, 2, 3, 4, 5, 6]
1values = [1, 2, 3, 4, 5, 6]2k = 3values this step[1, 2, 3, 4, 5, 6]valuesk ← 3
1values = [1, 2, 3, 4, 5, 6]2k = 33result = []values this step3kresult ← []
2k = 33result = []4for v in values:values this step[]resultv ← 1
3result = []4for v in values:5 result.append(v * k)values this step1vresult ← [3]
4for v in values:5 result.append(v * k)6print('RESULT:', result)values this step[] → [3]resultv ← 2
3result = []4for v in values:5 result.append(v * k)values this step1 → 2vresult ← [3, 6]
4for v in values:5 result.append(v * k)6print('RESULT:', result)values this step[3] → [3, 6]resultv ← 3
3result = []4for v in values:5 result.append(v * k)values this step2 → 3vresult ← [3, 6, 9]
4for v in values:5 result.append(v * k)6print('RESULT:', result)values this step[3, 6] → [3, 6, 9]resultv ← 4
3result = []4for v in values:5 result.append(v * k)values this step3 → 4vresult ← [3, 6, 9, 12]
4for v in values:5 result.append(v * k)6print('RESULT:', result)values this step[3, 6, 9] → [3, 6, 9, 12]resultv ← 5
3result = []4for v in values:5 result.append(v * k)values this step4 → 5vresult ← [3, 6, 9, 12, 15]
4for v in values:5 result.append(v * k)6print('RESULT:', result)values this step[3, 6, 9, 12] → [3, 6, 9, 12, 15]resultv ← 6
3result = []4for v in values:5 result.append(v * k)values this step5 → 6vresult ← [3, 6, 9, 12, 15, 18]
4for v in values:5 result.append(v * k)6print('RESULT:', result)values this step[3, 6, 9, 12, 15] → [3, 6, 9, 12, 15, 18]resultfor v in values:
3result = []4for v in values:5 result.append(v * k)stdout ← RESULT: [3, 6, 9, 12, 15, 18]
5 result.append(v * k)6print('RESULT:', result)values this stepRESULT: [3, 6, 9, 12, 15, 18]stdout
With NumPy
a * k multiplies every element of the array by the scalar k. NumPy treats
the scalar as if it were broadcast to match the array's shape — each element
is multiplied independently in the C loop.
library.py
import numpy as np
values = [1, 2, 3, 4, 5, 6]
k = 3
a = np.array(values)
result = a * k
print('shape:', result.shape)
print('dtype:', result.dtype)
print('values:', result.tolist())
print('RESULT:', result.tolist())
shape: (6,)
dtype: int64
values: [3, 6, 9, 12, 15, 18]
RESULT: [3, 6, 9, 12, 15, 18]
Implementation notes
- A scalar operand is broadcast to match the array's shape:
a * kis equivalent toa * np.full(a.shape, k). This scalar-to-array broadcast is the simplest case of the general broadcasting rules in ch06. - The same pattern works for all arithmetic operators:
a + k,a - k,a / k,a ** k. For in-place scaling without allocation, usea *= k. - Shape, dtype, and values are shown explicitly here because
ndarray.__repr__output varies with NumPy version and print options.