All courses › Intermediate Programming › NumPy array and vectorization

NumPy array and vectorization

A NumPy array looks like a list, but arithmetic operations act elementwise on the whole array without an explicit loop. This is called vectorization and is often 10–100 times faster than a Python loop over a plain list.

a∗ca * ceach element of aa multiplied by cc
a+ba + belementwise addition of equal-sized arrays

Symbols

a, ba,\ bNumPy arrays
cca scalar

Example

a = np.array([1, 2, 3])

a * 2 gives [2 4 6] — elementwise, not repetition as for a list.

list * 2 repeats a plain Python list, while array * 2 multiplies each number — do not mix them up.
Practise numerics with NumPy for free →

← Sorting and search · linspace and arange →

Part of Intermediate Programming: Numerics with NumPy.