Element-wise arithmetic
Operators like + - * / apply to every pair of elements at the same position automatically.
In this page:
Syntax
arr1 + arr2
arr1 - arr2
arr1 * arr2
arr1 / arr2
arr1 ** 2
arr * scalar
Element-wise arithmetic
With two arrays of the same shape, +, -, *, / and ** work element by element. With a scalar, the scalar is applied to every element. Note that * is not matrix multiplication; it multiplies matching elements.
Note:
Use // for floor division and % for remainders, also element-wise.
Example: Element-wise arithmetic
import numpy as np
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
print(a + b)
print(b - a)
print(a * b)
print(b / a)
print(a ** 2)
# Output:
# [11 22 33]
# [ 9 18 27]
# [10 40 90]
# [10. 10. 10.]
# [1 4 9]
Related Topics
Common Mistakes
- Expecting * to do matrix multiplication
- Combining arrays of incompatible shapes
- Integer arrays and / silently producing floats
Chapter Summary
- Operators work element-wise
- Scalars apply to all elements
- * is not the matrix product
- / always returns floats
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: