Introduction
Almost every data tool in Python, from pandas and scikit-learn to PyTorch and Matplotlib, is built on NumPy. It gives Python one thing it lacks: a compact, fast array of numbers. With NumPy, adding two million-element columns is one line, a + b, and runs in a few milliseconds, where a Python loop over lists takes a hundred times longer.
A note on how this course works. NumPy is a compiled library, and the Python that runs in your browser on this site carries only the standard library, so NumPy itself cannot run here. Each lesson therefore shows real NumPy code, with the output NumPy prints, and every example and exercise is a small plain-Python model of what NumPy does inside: how it stores an array, works out a shape, broadcasts, reduces along an axis. Building the model is the fastest way to understand the library, and everything you learn transfers directly when you run the real thing on your own computer.
How it works
A NumPy array, an ndarray, is a block of values that all have the same type, plus a shape that says how to read the block:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape) # (2, 3)
print(a.ndim) # 2
print(a.size) # 6
print(a)
# [[1 2 3]
# [4 5 6]]
A Python list of lists is different in two ways. Each element of a list is a separate Python object somewhere in memory, with its own type, and the list only stores references to them. And a list knows nothing about rows and columns: [[1, 2, 3], [4, 5, 6]] is just a list holding two other lists. An array stores the raw numbers next to each other in one block, and the shape turns that block into rows and columns.
That layout is why NumPy is fast. In a + b, NumPy checks the shapes once, then runs a tight loop in compiled C over the two blocks, with no Python objects created per element. This is a vectorised operation: you write the operation once, for the whole array, and it is applied element-wise. The habit of thinking in whole arrays instead of loops is the main skill this course teaches.
Example
# A tiny model of a NumPy array: a flat list of values plus a shape.
def make_array(rows):
flat = [x for row in rows for x in row]
return {"data": flat, "shape": (len(rows), len(rows[0]))}
def add(a, b): # what a + b does in NumPy: one operation, every element
assert a["shape"] == b["shape"], "shapes must match"
return {"data": [x + y for x, y in zip(a["data"], b["data"])], "shape": a["shape"]}
prices = make_array([[10, 20, 30], [40, 50, 60]])
taxes = make_array([[1, 2, 3], [4, 5, 6]])
total = add(prices, taxes)
print("shape:", total["shape"])
print("ndim:", len(total["shape"]))
print("size:", len(total["data"]))
print("data, row by row:", total["data"][:3], total["data"][3:])
# The same sum with plain lists of lists needs a loop inside a loop:
rows = [[10, 20, 30], [40, 50, 60]]
extra = [[1, 2, 3], [4, 5, 6]]
result = [[x + y for x, y in zip(r, e)] for r, e in zip(rows, extra)]
print("with lists:", result)Line by line
make_arrayflattens the rows into one list and records the shape, the way NumPy stores one block plus a shape.addchecks that the shapes match and adds the blocks element by element, likea + b.ndimis the length of the shape, andsizeis the number of values.- The plain-list version needs a loop inside a loop and knows nothing about shapes.
Expected result
shape: (2, 3)
ndim: 2
size: 6
data, row by row: [11, 22, 33] [44, 55, 66]
with lists: [[11, 22, 33], [44, 55, 66]]
In the real world
NumPy arrays hold images (height × width × colour), audio (samples), spreadsheets of measurements, and the weights of machine-learning models. pandas stores each column of a table as a NumPy array, and libraries such as SciPy and scikit-learn take and return arrays. Learning NumPy's model of data makes all of them easier.
Common mistakes
- Looping over an array element by element in Python, which throws away NumPy's speed.
- Expecting + to join arrays as it joins lists: for arrays it adds element by element.
- Confusing shape and size:
(2, 3)is the shape; 6 is the size. - Mixing types in one array, which silently converts everything to one type.
Best practices
- Think in whole arrays: ask what operation applies to every element at once.
- Check
shapeoften while you work; most NumPy bugs are shape bugs. - Keep arrays of one meaning, such as all prices, and let operations work on them whole.
Summary
NumPy's ndarray stores values of one type in one block of memory, read through a shape such as (2, 3). Operations on whole arrays run element-wise as fast compiled loops, replacing Python loops. In this course you read real NumPy code and build plain-Python models of what it does.
Quick check
Key takeaways
- A NumPy array holds values of one type in one block of memory, described by its shape.
- Whole-array operations replace Python loops and run tens to hundreds of times faster.
- This course shows real NumPy code and runs plain-Python models of what NumPy does.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.