NumPy and pandas basics
Vectorized operations
"Vectorized" means: write the operation once, apply it to every element at once — no explicit loop.
1Learn the idea
Read
Element-wise math
Every basic arithmetic operator works element-wise between an array and a number, or between two same-shaped arrays:
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
print(a + b) # [11 22 33]
print(a * 2) # [2 4 6]
print(b / a) # [10. 10. 10.]
Read
Comparisons produce boolean arrays
This is the idea that unlocks the next page's filtering:
scores = np.array([88, 92, 79, 95, 61])
print(scores > 80) # [ True True False True False]
scores > 80 doesn't return a single True or False — it returns a full array of the same shape, with the comparison applied element by element. Holding onto this is worth pausing on: a very common early mistake is expecting scores > 80 to behave like a single yes/no check.
Read
Aggregation methods reduce an array to a summary
Alongside element-wise operations, arrays have built-in methods that collapse many values into one:
print(scores.mean()) # average
print(scores.max()) # highest
print(scores.min()) # lowest
print(scores.sum()) # total
print((scores > 80).sum()) # count of elements above 80 — combines a comparison + aggregation
That last line is a pattern worth memorizing: a comparison (which produces True/False values) followed by .sum() counts how many were True, because Python treats True as 1 and False as 0 under the hood.
Read
Why loops still sneak in — and why to resist them
It's entirely possible to write a for loop over a NumPy array — Python won't stop you. But doing so throws away the entire performance benefit and usually produces longer, harder-to-read code:
Read
Works, but fights the library
curved = [] for s in scores: curved.append(s + 5)
Read
Idiomatic — same result, one line, and fast
curved = scores + 5
When you catch yourself writing a `for` loop over a NumPy array to do simple math, that's almost always a sign a vectorized equivalent exists — searching "numpy vectorized [operation]" is a reliable habit when you're not sure what it's called yet.
Go deeper
Before you start
Why this matters
If you wanted to add 5 to every score in a plain Python list, you'd write a loop (or a list comprehension, which is a loop in different clothes):
scores = [88, 92, 79]
curved = [s + 5 for s in scores]
With a NumPy array, you write the same idea without any loop at all:
import numpy as np
scores = np.array([88, 92, 79])
curved = scores + 5 # adds 5 to every element
scores + 5 isn't a typo or a shortcut for a hidden loop you don't see — NumPy pushes the addition down into fast, compiled code that operates on the whole block of memory at once. This is what "vectorized" means, and it's the single habit that separates comfortable NumPy code from code that fights the library.
In the wild
See how this idea shows up as a product and a company — then come back to the lesson. Skills transfer across vendors.