A hands-on introduction to NumPy, the library that pretty much all numerical and data work in Python is built on. Each topic is a short Jupyter notebook with small examples you can run, change and break.
Everything has been updated for NumPy 2.x.
| # | Notebook | What's in it |
|---|---|---|
| 0 | Data Types | dtypes, type codes, conversion, overflow, promotion, strings |
| 1 | Create an Array | array, arange, linspace, zeros/ones/full, eye, meshgrid |
| 2 | Inspect an Array | shape, size, memory use, strides |
| 3 | Sampling Methods | the Generator API, distributions, choice, shuffling, legacy API |
| 4 | Math Functions | what axis means, arithmetic, rounding, statistics, comparisons |
| 5 | Slicing & Indexing | slicing, boolean masks, fancy indexing, where, select |
| 6 | Sort an Array | sort, argsort, lexsort, partition, searchsorted |
| 7 | Manipulate an Array | reshape, transpose, flatten, adding/removing axes and elements |
| 8 | Combine & Split an Array | concatenate, stack, block, split, array_split |
| 9 | Set Operations | unique, intersection/union/difference, isin |
| 10 | Linear Algebra | @, norms, solve, lstsq, eigenvalues, QR, SVD |
| 11 | Broadcasting | the rules, practical uses, and common traps |
| 12 | Views and Copies | when changing one array changes another |
| 13 | Vectorization & Ufuncs | why loops are slow and how to get rid of them |
| 14 | Missing Values | NaN, inf, nan* functions, filling, masked arrays |
| 15 | Saving & Loading Arrays | .npy/.npz, memory mapping, CSV |
| 16 | Exercises | 17 practice problems with solutions |
If you'd rather read it all in one go, Complete Tutorial.ipynb has chapters 0 to 15 in a single notebook.
If you're completely new to NumPy, go through 0 to 5 in order, then read 11 (Broadcasting) and 12 (Views and Copies) before the rest. Those two explain most of the confusing behaviour you'll run into later. After that, pick whatever you need.
git clone https://github.com/eric496/numpy-tutorial.git
cd numpy-tutorial
pip install -r requirements.txt
jupyter labYou can also open any notebook directly in Google Colab via File → Open notebook → GitHub and pasting the repo URL.
Most examples use a seeded random generator (np.random.default_rng(seed)), so you should see the same numbers as the saved outputs. The exception is the timing examples in notebook 13, which depend on your machine.
The notebooks used to target NumPy 1.x. If you're following along with older code, these are the changes you're most likely to hit:
np.string_/np.unicode_are gone, usenp.bytes_/np.str_np.in1dis gone, usenp.isinnp.random.seed+np.random.randstill work, butnp.random.default_rng()is the recommended way- Python scalars no longer upcast arrays (
float32_array + 1.0staysfloat32)
The NumPy 2.0 migration guide has the full list.
Found a mistake or something that's unclear? Issues and pull requests are welcome.
If you edit a notebook, please re-run it top to bottom so the saved outputs match, and then regenerate the combined notebook:
python tools/build_complete.py- NumPy user guide
- NumPy: the absolute basics for beginners
- From Python to NumPy by Nicolas Rougier, a great next step once you're comfortable with the basics