Element-wise maths and broadcasting rules.
Arithmetic works element by element: a + b, a * 2, np.sqrt(a).
Broadcasting lets NumPy combine arrays of different shapes, e.g. adding a row to every row of a matrix.
Universal functions (ufuncs): np.add, np.power, np.round, np.exp, np.log.
💡 Key Takeaways: NumPy Math & Broadcasting
Mastering NumPy Math & Broadcasting helps you write clean and reliable NumPy code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
NumPy Math & Broadcasting Code Example
NumPy
import numpy as np
prices = np.array([100, 250, 400])
print(prices * 1.18)
print(np.sqrt(np.array([16, 25, 36])))
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m + np.array([10, 20, 30]))
Output
[118. 295. 472.]
[4. 5. 6.]
[[11 22 33]
[14 25 36]]
Free sign-in required · Google login available
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services