HOWTO · NumPy
How to Multiply Array With Scalar in Python
Learn how to multiply every element of a NumPy array by a scalar in Python with either the * operator or numpy.multiply(). See runnable examples and how broadcasting applies.
On this page
To multiply every element of a NumPy array by one scalar in Python, use either the * operator or numpy.multiply(). Both perform element-wise multiplication and return an array whose values are the corresponding products. The examples below show each form with its observed output.
Multiply a NumPy array by a scalar with *
Use * when the expression should read like ordinary arithmetic. NumPy applies the scalar to each array element, so the operation does not require a loop in your Python code. The variable result receives the computed array, while values remains available for later calculations.
Run this self-contained example in an environment with NumPy installed:
import numpy as np
values = np.array([2, 4, 6])
scale = 3
result = values * scale
print(result)
Observed output:
[ 6 12 18]
For an array [2, 4, 6] and scalar 3, the observed element-wise products are 6, 12, and 18. NumPy displays this integer array with spaces aligning its single-digit values. The same expression works with integer or floating-point values, with the result’s dtype following NumPy’s arithmetic and type-promotion rules.
Multiply a NumPy array by a scalar with numpy.multiply()
Call numpy.multiply(array, scalar) when naming the operation explicitly makes the code easier to read or compose. It takes the array and scalar as its two operands and performs the same element-wise multiplication as * for this use. Run this second self-contained example in an environment with NumPy installed:
import numpy as np
values = np.array([1.5, 2.0, 4.5])
scale = 2
result = np.multiply(values, scale)
print(result)
Observed output:
[3. 4. 9.]
Here each value is multiplied by 2, and the observed output is [3. 4. 9.]. NumPy prints these floating-point values without a trailing zero. Both examples assign the result to a new variable rather than changing the original array in place. If you need to retain the original values, keep using values; if you need a different workflow, assign the result back deliberately.
Choose between * and numpy.multiply()
For multiplying an array by a scalar, choose the spelling that makes the surrounding code clearest. The * operator is compact and familiar in an arithmetic expression. numpy.multiply() makes the operation explicit as a function call. Neither has a special advantage for broadcasting in this case: both use NumPy’s element-wise multiplication rules.
A scalar can be broadcast across the array because it can be applied to every element. More generally, NumPy can combine arrays with different shapes when their dimensions are compatible under broadcasting. Dimensions are compatible when they are equal or one of them is 1, comparing dimensions from the right; missing leading dimensions are treated as 1. See the NumPy broadcasting guide for the full rules and examples. For example, a (2, 3) array can be multiplied element-wise by a (3,) array, since the latter aligns with the last dimension. Shapes that fail these compatibility rules cannot be multiplied element-wise this way. Broadcasting does not make arbitrary shapes compatible.
These operations produce element-wise products; they do not perform matrix multiplication. For the scalar task, use either values * scale or numpy.multiply(values, scale), then inspect or use the returned array as needed.