ARRAYS AND NUMERICAL COMPUTING
NumPy Tutorials
NumPy is the array library the whole Python data stack is built on. These 43 tutorials cover it task by task — creating arrays, reshaping them, indexing them, and doing maths across millions of values without writing a loop.
- 43 tutorials
- 8 topics
- No loops needed
What NumPy is for
NumPy gives Python a real array type: a block of same-typed numbers stored together in memory. That sounds like a small thing next to a Python list, but it changes what is practical. Arithmetic on a million-element array happens in compiled C code, in one expression, with no for loop in sight.
This is called vectorisation, and it is the whole point of the library. (temps - 32) * 5 / 9 converts every temperature at once. The same idea covers sums, means, comparisons and filtering, so loops mostly disappear from numerical Python code once you are comfortable with it.
The other reason to learn NumPy is that it is the shared language underneath everything else. Pandas columns are NumPy arrays. Matplotlib plots them. scikit-learn expects them as input, and TensorFlow and PyTorch tensors convert straight to and from them. Shape errors are the most common thing to get stuck on, which is why reshaping has its own section below.
Install it and make an array
NumPy is a single pip install with no other dependencies. If you have already installed Pandas, SciPy, scikit-learn or Matplotlib, you have NumPy too — they all depend on it.
The first thing to get familiar with is .shape. Almost every NumPy error you will hit is a shape mismatch, and printing the shape of each array is usually enough to see what went wrong.
ValueError: setting an array element with a sequence almost always means your nested lists are ragged — the rows are different lengths, so NumPy cannot make a rectangular array out of them. Check the length of each row before converting.
If you are starting today
A sensible order to learn NumPy in
Five steps. Shape comes early on purpose — it is behind most of the errors beginners get stuck on.
- 1
Create an array
From a list, and with
zeros,linspaceand the random functions for test data. - 2
Understand shape
What
(3, 4)means, and how to read the shape mismatch in an error message. - 3
Index and slice it
Single elements, ranges, whole rows and columns, and boolean masks for filtering.
- 4
Do maths across it
Sums, means and rounding, along a whole array or down a single axis.
- 5
Reshape and combine
Reshaping, joining arrays together and splitting them apart again.
Quick reference
The NumPy calls you will use most
Fourteen lines covering array creation, shape, indexing and the maths — which is most of what NumPy is used for day to day.
| Task | Code | Worth knowing |
|---|---|---|
| From a list | np.array([1, 2, 3]) | All values are coerced to one dtype. |
| Zeros or ones | np.zeros((3, 4)) | Handy for preallocating a result array. |
| A range | np.arange(0, 10, 2) | Like range(), but an array. |
| Evenly spaced | np.linspace(0, 1, 5) | Count, not step — good for plot axes. |
| Random values | np.random.default_rng().random(5) | The modern generator API, not np.random.rand. |
| Check the shape | arr.shape | The first thing to print when debugging. |
| Reshape | arr.reshape(3, 4) | Element count has to match exactly. |
| Single element | arr[0, 2] | Row first, then column. |
| Slice | arr[1:4] | A view of the original, not a copy. |
| Boolean filter | arr[arr > 10] | Returns only the matching values. |
| Sum down an axis | arr.sum(axis=0) | axis=0 collapses rows, 1 collapses columns. |
| Average | arr.mean() | np.nanmean() skips NaN values. |
| Join two arrays | np.concatenate([a, b]) | Shapes must line up on the other axes. |
| Back to a list | arr.tolist() | Gives real Python numbers, not NumPy scalars. |
Every tutorial, by topic
Every NumPy tutorial, grouped by what you are trying to do
Sorted into the eight jobs that make up nearly all NumPy work, instead of the single flat list this page used to be.
Creating arrays
9Every way to make an array: from lists, empty and zero-filled, evenly spaced with linspace, matrices, and random data for testing.
Shape and reshaping
4Reading .shape, reshaping between 1D, 2D and 3D, and the filtering that changes shape as a side effect. Start here when an error mentions dimensions.
Indexing and slicing
5Getting at single elements, ranges, rows and columns, plus replacing values in place and finding the smallest element.
Maths and statistics
9Sums, averages, rounding, absolute values, differences, normalising, matrix maths and element-wise arithmetic — all without loops.
Combining and splitting
4Joining arrays with concatenate, splitting them apart, repeating values and reducing to unique ones.
Types and conversion
3NumPy’s dtypes and converting arrays to and from Python lists, including lists of strings.
Reading and writing files
3Loading numeric data from CSV with read_csv and genfromtxt, and saving arrays back out with savetxt.
More NumPy tutorials
6Counting, sorting, reversing, the import error you hit when NumPy is not found, and the classic ragged-array ValueError.
Keep going
What to learn next to it
NumPy is the layer underneath. These are the libraries that sit on top of it.
Questions people ask
Frequently asked questions
What does NumPy do that Python lists cannot?
Two things. It stores numbers of one type in a contiguous block of memory, which makes it far faster and more compact, and it applies arithmetic to every element at once, so arr * 2 doubles a million values in one expression. Doing that with a list needs a loop or a comprehension.
What does the shape (3, 4) mean?
Three rows and four columns — 12 values in total. Shape is always a tuple read outermost first, so (2, 3, 4) is two blocks of three rows of four values. When an error complains about shapes not aligning, printing .shape for each array usually shows the problem immediately.
How do I fix ‘ValueError: setting an array element with a sequence’?
Your nested lists are not all the same length, so they do not form a rectangle. Check the length of every row. If the rows genuinely differ, you need a list of arrays or an object-dtype array rather than a normal NumPy array.
Is NumPy faster than Pandas?
For pure numeric maths on a single block of numbers, yes — Pandas adds an index and column labels, which cost something. But Pandas is the right tool when your data has named columns of mixed types. Use NumPy underneath it for the heavy arithmetic.
Do I need NumPy if I already use Pandas?
You already have it, since Pandas depends on it. Learning it directly pays off when you need element-wise maths across columns, when you are preparing data for scikit-learn or PyTorch, or when you want to understand what a dtype or shape error is telling you.
What is the difference between reshape and resize?
reshape returns a new view of the same data in a different shape and needs the element count to match exactly. resize changes the array in place and will pad or trim values to fit. Reach for reshape unless you specifically want the data altered.
Make one array and do maths on it
Create an array, check its shape, run one calculation across it. That is the whole mental model, and it takes four lines.
