TExcelTAD
Home › NumPy › NumPy Intro

NumPy Intro

NumPy (short for Numerical Python) is the library that turned Python into a serious tool for science, engineering and data analysis. At its heart is one object, the ndarray: a grid of values, all of the same type, stored in one compact block of memory. Almost every data library you will meet later (pandas, Matplotlib, SciPy, scikit-learn, PyTorch) either is built on NumPy arrays or speaks their language.

python · try it yourself
import numpy as np

temperatures = np.array([18.5, 21.0, 23.4, 19.8, 25.1])
print(temperatures)
print("Average:", temperatures.mean())
print("In Fahrenheit:", temperatures * 9 / 5 + 32)
[18.5 21.  23.4 19.8 25.1]
Average: 21.560000000000002
In Fahrenheit: [65.3  69.8  74.12 67.64 77.18]

Notice the last line: we multiplied the whole array by a number, with no loop. That style, called vectorised code, is the main idea of NumPy.

Why not just use Python lists?

Python lists are flexible: each item can be anything, so every item is a separate Python object stored somewhere in memory, and the list holds pointers to them. That flexibility has a cost.

Python list NumPy ndarray
Item types Any mix of types One type for all items (int64, float64, …)
Memory layout Pointers to scattered objects One contiguous block of raw numbers
Maths on all items Needs a loop or comprehension One expression: a * 2, np.sqrt(a)
Speed for numeric work Slow (interpreted loop) Fast (compiled C loops)
Dimensions Nested lists, by convention Real n-dimensional shape: (rows, cols, …)

The difference shows up as soon as you try to do arithmetic:

python · try it yourself
import numpy as np

prices = [4.0, 2.5, 10.0]
print(prices * 2)            # list: repeats the list!

prices_arr = np.array(prices)
print(prices_arr * 2)        # array: doubles every price

# Doing it with a list needs a comprehension
print([p * 2 for p in prices])
[4.0, 2.5, 10.0, 4.0, 2.5, 10.0]
[ 8.  5. 20.]
[8.0, 5.0, 20.0]

Where is NumPy used?

  • Data analysis: pandas stores each column of a DataFrame as a NumPy array.
  • Images: a colour photo is just a 3-D array of shape (height, width, 3).
  • Machine learning: datasets, model weights and predictions are arrays.
  • Simulation and statistics: random number generators, distributions, linear algebra.

Here is a tiny taste of that "image is an array" idea, a 4×6 picture made of brightness values:

python · try it yourself
import numpy as np

image = np.zeros((4, 6), dtype=int)   # 4 rows, 6 columns, all black (0)
image[1:3, 2:4] = 9                   # paint a bright square in the middle
print(image)
print("shape:", image.shape, "pixels:", image.size)
[[0 0 0 0 0 0]
 [0 0 9 9 0 0]
 [0 0 9 9 0 0]
 [0 0 0 0 0 0]]
shape: (4, 6) pixels: 24

Who makes NumPy?

NumPy is open-source software, created in 2005 by Travis Oliphant, who merged two older array packages (Numeric and Numarray). Today it is developed by a large community and funded through NumFOCUS. The fast core is written in C; the parts you call are Python. The source lives on GitHub at github.com/numpy/numpy.

Note: This course runs NumPy 2.2 inside your browser (through Pyodide, a WebAssembly build of Python). Everything you see in the examples is real output. NumPy 2 changed a few details compared with the 1.x series, for example how scalar values are shown with repr(); we point those out as we go.

What you will learn

  1. Arrays: creating, indexing, slicing, data types, shapes, joining, splitting, searching, sorting and filtering.
  2. Random numbers: the modern Generator API and the classic probability distributions, with plots.
  3. ufuncs: NumPy's fast element-wise functions for arithmetic, rounding, logs, trigonometry and set operations.
  4. Beyond the basics: broadcasting, aggregations along an axis, linear algebra, structured arrays, missing data and writing fast vectorised code.

Test yourself

What is the core data structure provided by NumPy?

  1. The DataFrame
  2. The ndarray
  3. The linked list
  4. The Series

What does [1, 2] * 2 give for a plain Python list?

  1. [2, 4]
  2. [1, 2, 1, 2]
  3. An error
  4. 4

Why are NumPy arrays faster than lists for numeric work?

  1. They are written in JavaScript
  2. All items share one type and sit in one block of memory, processed by compiled loops
  3. They cache results on disk
  4. They use multiple Python interpreters