← Back to NumPy Course | Chapter 6: Math Operations | Lesson 1 of 7

Element-wise arithmetic

Operators like + - * / apply to every pair of elements at the same position automatically.

In this page:

  1. Element-wise arithmetic
Syntax
python
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

python
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
  1. Expecting * to do matrix multiplication
  2. Combining arrays of incompatible shapes
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.