Chapter 3: Simple Arithmetic

simple arithmetic ufuncs in NumPy — written as if I’m sitting next to you at the computer, explaining slowly, showing small realistic examples, comparing with pure Python, answering typical student questions, and helping you understand how these basic operations become extremely powerful when used correctly.

Let’s pretend we’re working together in a notebook.

Python

1. The four basic arithmetic ufuncs you already use every day

These four symbols are actually ufuncs when used with NumPy arrays:

text

There are also related integer-only versions:

text

Very important sentence:

When you write a + b, a * 2, arr ** 3 or data / 10 with NumPy arrays → you are already using ufuncs.

2. Basic examples – scalar vs array

Python

Student question I always get:

“Why does a + 10 add 10 to every element?”

Answer: Because + is a ufunc. NumPy sees that one operand is an array and the other is a scalar → it automatically broadcasts the scalar to the shape of the array.

3. Array vs array arithmetic

Python

Important detail: Division a / b always returns floating-point results, even if both arrays contain integers.

Python

If you want integer division (floor division):

Python

4. Broadcasting – the magic that makes arithmetic ufuncs so powerful

NumPy automatically stretches smaller arrays to match the shape of larger ones.

Python

Quick broadcasting rule (memorize this):

Shapes are compared from the right to the left. Two dimensions are compatible if:

  • they are equal, or
  • one of them is 1 (it gets stretched)

5. Realistic patterns – arithmetic ufuncs in real code

Pattern 1 – Normalize / standardize data

Python

Pattern 2 – Scale pixel values to [0,1]

Python

Pattern 3 – Compute percentage change

Python

Pattern 4 – Element-wise clipping / bounding

Python

Pattern 5 – Simple financial return calculation

Python

6. Summary – Arithmetic ufuncs Quick Reference

Symbol / Operation ufunc name Notes / Common use
  • | np.add | a + b, a + scalar
  • | np.subtract | a – b
  • | np.multiply | a * b, a * 2 / | np.true_divide | always float result // | np.floor_divide | integer division % | np.mod, np.remainder | remainder ** | np.power | a ** 2, a ** b np.clip(a, min, max) | – | bound values np.round / floor / ceil | – | rounding

Final teacher messages (very important)

Golden rule #1 If you are writing a for loop to do simple arithmetic on every element of a NumPy array → you are almost always doing it wrong. Just write a + b, a * 5, arr ** 2 directly.

Golden rule #2 When you write a + 10, arr / 255.0, data – mean → you are already using ufuncs + broadcasting. That’s why NumPy feels so fast and expressive.

Golden rule #3 Use np. prefix when you want to be explicit (np.add(a, b) instead of a + b) — especially in larger code or when teaching others.

Would you like to continue with any of these next?

  • Broadcasting in more depth (3D/4D arrays, common mistakes)
  • Using arithmetic ufuncs with out= parameter (in-place)
  • Combining arithmetic with masking & np.where
  • Realistic mini-project: vectorized financial calculation or image processing
  • Difference between true_divide vs floor_divide vs remainder

Just tell me what feels most useful for you right now! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *