Introduction
NumPy (Numerical Python) is a library built around one core idea: a fast, typed array that stores its data as one contiguous block of memory, and a set of operations that run on the whole array at once instead of looping over it in Python.
Why not just use a list?
A Python list can hold anything — strings, numbers, other lists — mixed together, which means Python has to check each element's type individually every time you touch it. A NumPy array holds one fixed type for every element, packed together in memory, so operations on it run in fast, compiled C loops instead of the Python interpreter's own loop. That difference is most of why NumPy exists.
import numpy as np numbers = np.array([1, 2, 3, 4, 5]) print(numbers) print(type(numbers)) print(numbers.dtype)
[1 2 3 4 5] <class 'numpy.ndarray'> int64
np.array() takes a regular Python list and converts it into an ndarray (n-dimensional array), NumPy's core data type. numbers.dtype tells you the single data type every element is stored as — here, 64-bit integers.
Operations run on the whole array at once
This is the payoff. With a plain list, converting prices to include tax means writing a loop. With a NumPy array, you write the math once and it applies to every element:
import numpy as np prices = np.array([10, 20, 30]) with_tax = prices * 1.08 print(with_tax)
[10.8 21.6 32.4]
No loop, no index variable — prices * 1.08 multiplies every element by 1.08 and hands back a new array. This pattern, called vectorization, is the style you'll write in for the rest of this course.
np.array([1, 2, 3], dtype=np.int64).