Why NumPy arrays beat Python lists for math (10x to 100x speedups)
NumPy is the foundation library because a NumPy array stores numbers in a contiguous block of typed memory, while a Python list stores pointers to boxed PyObjects scattered across the heap. The result: array arithmetic runs in C-level loops, list arithmetic runs in Python-level loops with type checks on every element. A multiply on a 1-million-float array takes about 3 ms with NumPy and around 200 ms with a list comprehension on the same machine.
Vectorization is the mental model. Replace explicit `for` loops with whole-array operations. `arr * 2` multiplies every element. `np.sqrt(arr)` applies the square root elementwise. `(arr - arr.mean()) / arr.std()` standardizes a column without writing a loop. The shape of the array determines what the operation does, and broadcasting rules align shapes automatically when they differ along axes of length 1.
Indexing is the second pillar. NumPy supports integer indexing (`arr[3]`), slice indexing (`arr[2:7]`), boolean masks (`arr[arr > 5]`), and fancy indexing (`arr[[0, 2, 5]]`). Mask-based selection is the workhorse for filtering: students writing CS50P or DATA 100 problem sets use it to count rows that satisfy a predicate, often nested with `&` and `|` for compound conditions. Note the bitwise operators, not `and` / `or`: NumPy arrays raise an ambiguity error if you use the keyword forms.
# Requires: pip install numpy
import numpy as np
# Array creation from list, plus zeros and ranges
heights_cm = np.array([162, 175, 168, 181, 170, 158, 177])
weights_kg = np.array([55, 78, 64, 85, 72, 50, 80])
# Vectorized BMI for the whole sample, no loop
bmi = weights_kg / (heights_cm / 100) ** 2
print("BMI values:", np.round(bmi, 2))
# Boolean mask: students with BMI above 25
overweight_mask = bmi > 25
print("Count above 25:", overweight_mask.sum())
print("Their heights:", heights_cm[overweight_mask])
# Aggregations
print(f"Mean BMI: {bmi.mean():.2f}, Std: {bmi.std():.2f}")
print(f"Min: {bmi.min():.2f}, Max: {bmi.max():.2f}")