NumPy Cheat Sheet and Next Steps

Lesson 17 of 17

You have covered the core of NumPy. This page recaps the essentials on one screen, gives you a practice exercise that uses most of the course, and points you to what to learn next.

Cheat sheet

Task

Code

Create an array

np.array([1, 2, 3])

Ranges of numbers

np.arange(0, 10, 2), np.linspace(0, 1, 5)

Shape and size

arr.shape, arr.size, arr.ndim

Change the type

arr.astype(float)

Reshape

arr.reshape(2, -1)

Index and slice

arr[0], arr[1:4], arr[0, 1]

Filter

arr[arr > 5]

Join and split

np.concatenate(), np.array_split()

Sort and search

np.sort(), np.where()

Math

arr + 1, np.sqrt(arr), arr.sum(axis=0)

Statistics

np.mean(), np.median(), np.std()

Random numbers

np.random.default_rng(42)

Linear algebra

A @ B, np.linalg.solve(A, b)

Practice: put it all together

Seven days of temperatures for three cities. Try to predict each line of output before you read it:

Example

Python
import numpy as np

rng = np.random.default_rng(7)
temps = rng.integers(20, 41, size=(7, 3))  # 7 days x 3 cities

print(temps)
print("Average per city:", temps.mean(axis=0).round(1))
print("Hot days (35+) per city:", (temps >= 35).sum(axis=0))
print("Hottest day for each city:", temps.argmax(axis=0))

Output

Plain Text
[[39 33 34]
 [38 32 36]
 [37 24 21]
 [26 25 38]
 [39 20 30]
 [37 22 36]
 [22 29 37]]
Average per city: [34.  26.4 33.1]
Hot days (35+) per city: [5 0 4]
Hottest day for each city: [0 0 3]

Go deeper

The Complete NumPy Guide on this site goes much further, with performance tips, real-world pipelines and interview questions:

What to learn next

  • pandas — tables with labelled rows and columns, built on NumPy. See the pandas tutorials.

  • Matplotlib — turn NumPy arrays into charts.

  • scikit-learn — machine learning models that take NumPy arrays as input.