NumPy Math & Broadcasting

Element-wise maths and broadcasting rules.

✍️ 3 exercises + code challenge in this lesson

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

NumPy Math & Broadcasting Practice Exercises

Check what you learned in “NumPy Math & Broadcasting”.

/ 3 solved
Fill in the blank

Square root of every element.

np.(arr)
✓ Correct! Not quite. Try again.
Multiple choice

What is broadcasting?

✓ Correct! Not quite. Try again.
Code challenge

Multiply every price by 2 and print the result as a list.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

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