Learn NumPy

Fast n-dimensional arrays and the numerical foundation of Python

27 lessons · 6 hours · 4 levels · covers NumPy 2.x

NumPy provides the ndarray, a fast typed n-dimensional array, together with vectorized math, broadcasting, linear algebra and random numbers. It is the foundation that pandas, SciPy, scikit-learn and most of scientific and data Python are built on. Learning it turns slow Python loops into concise, fast array expressions.

Before you start

NumPy builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics9 lessons · 2 hours in total
2. PracticalUse it productively at work, with the usual idioms17 lessons · 3 hours in total
3. AdvancedThe less common features, performance and trade-offs23 lessons · 5 hours in total
4. ExpertInternals and the edge cases experts know27 lessons · 6 hours in total

The NumPy tree

NumPyThe ndarrayThe ndarrayCreating arraysCreating arraysData types (dtypes)Data types (dtypes)Shape and reshapeShape and reshapeIndexing and slicingIndexing and slicingVectorized arithmeticVectorized arithmeticUniversal functions (ufuncs)Universal functions(ufuncs)Aggregations and the axis argumentAggregations and theaxis argumentBoolean masksBoolean masksBroadcastingBroadcastingFancy (integer array) indexingFancy (integer array)indexingCopies vs viewsCopies vs viewsRandom numbers with GeneratorRandom numberswith GeneratorLinear algebra basicsLinear algebra basicsSorting, searching and uniqueSorting, searchingand uniqueStacking and splittingStacking and splittingSaving and loading arraysSaving and loadingarraysListsListsModules and importsModules and importsNumbers and arithmeticNumbers andarithmeticfor and while loopsfor and while loopsNames, references and mutabilityNames, referencesand mutabilityReading and writing filesReading and writingfiles
Essentials and Practical shown (17 of 27 lessons); the full list is below. Click a lesson to read it.

All 27 lessons, in order

Arrays and dtypes

  1. The ndarray · Essentials · 10 min

    NumPy's core object: a fixed-size, typed, n-dimensional grid of numbers stored in one block of memory.

  2. Creating arrays · Essentials · 8 min

    Build arrays without typing values: zeros, ones, full, arange, linspace and the *_like helpers.

  3. Data types (dtypes) · Essentials · 12 min

    Every array has one dtype that fixes how many bytes each element uses and how they are interpreted.

  4. Shape and reshape · Essentials · 10 min

    Change how the same data is arranged into axes with reshape, ravel, transpose and new axes.

  5. Structured arrays · Advanced · 12 min

    Arrays whose elements are records with named, typed fields, matching C structs and binary formats.

Indexing and selection

  1. Indexing and slicing · Essentials · 12 min

    Pick elements, rows, columns and sub-blocks with integer indexes and start:stop:step slices per axis.

  2. Boolean masks · Essentials · 10 min

    Select, count and modify elements that meet a condition using boolean arrays as indexes.

  3. Fancy (integer array) indexing · Practical · 12 min

    Index with arrays of positions to gather, reorder or scatter arbitrary elements.

Computing with arrays

  1. Vectorized arithmetic · Essentials · 10 min

    Replace Python loops with whole-array expressions that run in compiled code.

  2. Universal functions (ufuncs) · Essentials · 10 min

    np.sqrt, np.exp, np.maximum and friends: fast element-wise functions with extra powers like out= and where=.

  3. Aggregations and the axis argument · Essentials · 12 min

    Reduce arrays with sum, mean, min, max, std, argmax, and choose which axis to collapse.

  4. Broadcasting · Practical · 15 min

    The rules that let arrays of different shapes combine element-wise without copying data.

  5. einsum: Einstein summation · Advanced · 15 min

    Express dot products, transposes, traces and batched contractions in one subscript string.

Memory and performance

  1. Copies vs views · Practical · 12 min

    Know which operations share memory with the original array and which make independent copies.

  2. Memory layout and strides · Advanced · 15 min

    How shape, strides and C/Fortran order map n-dimensional indexes onto one flat buffer.

  3. Vectorization patterns and performance · Advanced · 18 min

    Turn loop-heavy code into array code: masks, cumsum/diff, bincount, reduceat, and avoiding temporaries.

Everyday toolbox

  1. Random numbers with Generator · Practical · 10 min

    Use np.random.default_rng for reproducible, high-quality random numbers and sampling.

  2. Linear algebra basics · Practical · 15 min

    Matrix products with @, solving systems, inverses, norms, decompositions and batched matrices.

  3. Sorting, searching and unique · Practical · 12 min

    sort, argsort, searchsorted, partition and unique: ordering data and finding things quickly.

  4. Stacking and splitting · Practical · 10 min

    Join arrays with concatenate and stack, and cut them apart with split and array_split.

  5. Saving and loading arrays · Practical · 10 min

    Persist arrays with .npy/.npz, read text files with loadtxt/genfromtxt, and memory-map big files.

Numerics and missing data

  1. Floating point and numerical precision · Advanced · 15 min

    Rounding error, machine epsilon, comparing floats, cancellation and summation accuracy in NumPy.

  2. Missing data: NaN and masked arrays · Advanced · 12 min

    Represent and compute around missing values with NaN, nan-aware functions and numpy.ma.

Internals and interop

  1. The C array object and buffer protocol · Expert · 20 min

    What an ndarray is in C (data pointer, dims, strides, descr, base, flags) and how it shares memory with other code.

  2. Ufunc internals and custom ufuncs · Expert · 20 min

    Type resolution, inner loops, the iterator, generalized ufuncs, and ways to build your own.

  3. Interop: array protocols and the array API · Expert · 18 min

    How other array libraries plug into NumPy (__array__, __array_ufunc__, __array_function__) and how to write library-agnostic code.

  4. SIMD, threading and BLAS · Expert · 18 min

    Where NumPy's speed comes from: CPU-dispatched SIMD loops, multithreaded BLAS, the GIL and free-threaded Python.