How to use the NumPy Cheat Sheet
- 1. Search or browseType what you want to do, or pick one section of the NumPy cheat sheet.
- 2. CopyEvery entry has its own Copy button, so it goes straight into your editor or notes.
- 3. DownloadSave the whole NumPy cheat sheet as plain text or Markdown for offline revision.
What's in it
Every snippet assumes import numpy as np. Results are shown the way print() displays them. The 45 snippets are organised into 8 sections: Creating arrays, Shape & type, Indexing, Maths, Broadcasting, Statistics, Joining & sorting, Random & linear algebra.
Every snippet that shows a result (marked with →) was run with NumPy 2.x, and the printed output matches what's shown.
Example
axis confuses almost everyone at first. Take m = np.array([[1, 2], [3, 4]]). m.sum() → 10 adds everything. m.sum(axis=0) → [4 6] collapses the rows, leaving one total per column (1+3 and 2+4). m.sum(axis=1) → [3 7] collapses the columns, leaving one total per row. Think of axis as the direction that disappears.
Tips for using it
- Check
.shapeafter every reshape or operation while you learn; most NumPy bugs are shape bugs. - Use
np.random.default_rng(seed)rather than the older np.random.seed for repeatable experiments. - Boolean masks like
a[a > 10]replace most loops with an if inside.
A quick look, one entry per section
- Creating arrays:
np.array([1, 2, 3])(array from a list) - Shape & type:
a = np.arange(6); a.shape(shape is a tuple) - Indexing:
a = np.arange(10); a[2:7:2](slice start:stop:step) - Maths:
np.array([1, 2, 3]) * 10(operations apply to every item (no loop needed)) - Broadcasting:
np.arange(3).reshape(3, 1) + np.arange(3)(shapes (3,1) and (3,) stretch to (3,3)) - Statistics:
m = np.array([[1, 2], [3, 4]]); m.sum()(sum of everything) - Joining & sorting:
np.concatenate([[1, 2], [3]])(join arrays end to end) - Random & linear algebra:
rng = np.random.default_rng(42); rng.integers(1, 7, size=3).shape(modern random generator with a seed for repeatable results)
Search the box above for the rest of the NumPy entries, or download the full sheet.
Frequently asked questions
Which NumPy version?
The results were produced with NumPy 2.x. Printed values are the same in 1.x for these snippets, though scalar repr changed in 2.0 (np.int64(10) instead of 10 when not printed).
Is pandas covered?
No. See the separate pandas cheat sheet; pandas is built on NumPy.
Is the NumPy cheat sheet free?
Yes. You can search, read and copy every entry in the NumPy cheat sheet without signing up. The first time you download it, we ask once for your name and WhatsApp number; after that downloads and every other Uncodemy tool open straight away.