Quick Reference · array programming with Python

numpy cheat sheet

Everything in NumPy revolves around one object — the ndarray — and a small set of ideas that repeat everywhere: shape, axis, broadcasting, and views vs. copies. Learn those four once and the 200+ functions stop being a list to memorize.

create / load inspect index & search shape & combine math & linalg aggregate / stats gotcha most common

Distilled & cross-checked across: numpy.org/doc · DataCamp · Dataquest · GeeksforGeeks · Real Python · TutorialsPoint · vanitaAI · Ontario Tech SLC

Anatomy of an ndarray
axis=1 (columns) → axis=0 (rows) ↓ 1 2 3 4 5 6 7 8 9 10 11 12 a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) a[1, 2] → 7 a.ndim2 a.shape(3, 4) a.size12 a.dtypeint64 a.itemsize8 bytes a.nbytes96 nbytes = size × itemsize (12 × 8 = 96)
01Setup & Importcreate
02Array Creationcreate
03Attributes & Inspectioninspect
04Indexing & Slicingindex & search
05Boolean & Fancy Indexingindex & search
06Reshaping & Transposingshape & combine
07Combining & Splittingshape & combine
08Adding, Removing & Uniqueshape & combine
09Copies vs. Viewsinspect
10Arithmetic & Universal Functionsmath & linalg
11Broadcastingmath & linalg
12Aggregations & Statisticsaggregate / stats
13Sorting & Searchingindex & search
14Linear Algebranp.linalg
15Random NumbersGenerator API
16Comparison & Logical Opsmath & linalg
17Set Operationsshape & combine
18Save, Load & I/Oinspect
19Performance Tips & Gotchashandle with care
Common dtypesanywhere you see dtype=
Axis Quick-Readanywhere you see axis=

Axis reduction & broadcasting, visually

Same array, four different results — depending on which axis you collapse (or don't). Based on the official NumPy quickstart/broadcasting reference diagrams.

arr.sum(axis=0)

Collapses the rows: walk down each column and add. A 2×3 array becomes a length-3 row.

1 2 3 4 5 6 5 7 9

arr.sum(axis=1)

Collapses the columns: walk across each row and add. A 2×3 array becomes a length-2 column.

1 2 3 4 5 6 6 15

arr.sum() — no axis

Default behavior: flatten completely first, then reduce to a single scalar.

1 2 3 4 5 6 21

Broadcasting: (2,3) + (3,)

A shorter shape stretches across the missing dimension — no data is actually copied.

1 2 3 4 5 6 + 10 20 30 10 20 30 = 11 22 33 row vector reused for every row

Worth memorizing

copy vs viewslices are views; boolean/fancy indexing makes a copy
arr += 1in-place op on an int array silently truncates float math
axis=0 vs axis=10 walks down rows (per-column); 1 walks across columns (per-row)
* vs @* is elementwise; @/matmul is true matrix multiply
reshape()returns a view when possible, silently falls back to a copy
broadcasting rulecompare shapes trailing-dim first; dims must match or be 1
NaN != NaNuse np.isnan(), never arr == np.nan
default_rng()the modern random API — prefer it over legacy np.random.seed()