NumPy Cheatsheet - NumPy Functions & Array Operations Reference

All essential NumPy commands organized by use case, with 40+ entries you can copy and run directly. Find the right command fast when you need it.

Languages·40 commands·Last updated 2026-07-21
Back to Languages

Array Creation 9

np.array([1, 2, 3])
Create array from a list
np.zeros((3, 4))
3x4 array of zeros
np.ones((2, 3))
2x3 array of ones
np.arange(0, 10, 2)
Evenly spaced array [0, 2, 4, 6, 8]
np.linspace(0, 1, 5)
5 points evenly spaced from 0 to 1
np.eye(3)
3x3 identity matrix
np.random.rand(3, 3)
3x3 uniform random numbers
np.random.randn(3, 3)
3x3 standard normal random numbers
np.random.randint(0, 10, (2, 3))
2x3 random integers

Indexing & Slicing 6

a[0]
First element
a[1:3]
Slice: take items 2 and 3
a[a > 5]
Boolean indexing: items > 5
a[[0, 2, 4]]
Fancy indexing: selected positions
a[1:3, 2:4]
2D slice: rows 1-2, cols 2-3
np.where(a > 5, a, 0)
Conditional replace: keep > 5, else 0

Shape Manipulation 8

a.shape
View array shape
a.reshape(2, 3)
Reshape to 2x3
a.flatten()
Flatten to 1D
a.T
Transpose
np.concatenate([a, b], axis=0)
Concatenate along rows
np.vstack([a, b])
Stack vertically
np.hstack([a, b])
Stack horizontally
np.split(a, 3)
Split into 3 equal parts

Math & Statistics 10

a.sum()
Sum all elements
a.mean()
Mean
a.std()
Standard deviation
a.min() / a.max()
Min / max
a.argmin() / a.argmax()
Index of min / max
a.cumsum()
Cumulative sum
np.dot(a, b)
Matrix dot product
a @ b
Matrix multiplication (Python 3.5+)
np.sort(a)
Sort
np.unique(a)
Unique values

Broadcasting & Saving 7

a + 1
Broadcast: add 1 to every element
a * 2
Broadcast: multiply every element by 2
a + b (compatible shapes)
Broadcasted arithmetic (compatible shapes)
np.save("data.npy", a)
Save as .npy file
np.load("data.npy")
Load .npy file
np.savetxt("data.csv", a, delimiter=",")
Save as CSV
np.loadtxt("data.csv", delimiter=",")
Load from CSV

Tips

  • NumPy arrays are homogeneous - all elements share one dtype; mixing types is auto-promoted.
  • Broadcasting rule: compare dimensions from the rightmost; they must be equal or one of them is 1.
  • a * b is element-wise; use a @ b or np.dot(a, b) for matrix multiplication.

Official References

Commands are compiled from the official docs below. Click to verify the latest usage.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us