NumPy · lesson 1 of 13
Parts in this lesson
Aimport numpy as npthe usual way to load NumPy
Bnp.array(prices)an array from a list
Carr.dtypethe one type every number in it has
D%timeit1.3: how long a line takes

Why arrays

Last updated

Every tool in the rest of this course, pandas, the chart libraries and the statistics, is built on one thing: the NumPy array. In this lesson you'll see what an array is, how it differs from a Python list, and why that difference makes it so much faster.

By the end of this lesson you can

  • Make a NumPy array from a list and do maths on all of it at once
  • Explain how an array stores its numbers differently from a list
  • Time a Python loop against np.sum with %timeit
  • Say why * means something different on a list and on an array

A list against an array

In Stage 1 you kept prices in a Python list. NumPy keeps them in an array. Run the cells and compare.

first-array.ipynb

import numpy as np loads NumPy under the short name np. Everyone writes it that way, so you'll see np. in every example you find.

The second cell is the point: arr * 1.12 adds 12% VAT to every price at once. With a list you need a loop, or a list comprehension like the third cell, to visit each price yourself.

The answers look different, but they're the same numbers. 50.400000000000006 is how a computer stores 45 × 1.12 (lesson 1.1 showed why decimals aren't exact). NumPy just rounds what it shows to 8 digits, so the array looks tidier.

One type, packed together

Why can an array do maths on everything at once? Because of how it keeps the numbers. Step through:

How a list and an array keep five prices. A Python list doesn’t hold the numbers themselves. Each box holds a pointer: the address of a number stored somewhere else.•••••int 45int 46int 45int 44int 47prices = [45, 46, 45, 44, 47]
1 of 4 A Python list doesn’t hold the numbers themselves. Each box holds a pointer: the address of a number stored somewhere else.

The last cell of the notebook above asked arr.dtype, the array's data type. Every number in an array has the same one. That's what lets NumPy pack them side by side and treat them as one block.

You'll see int64 on your own computer: whole numbers, 64 bits (8 bytes) each. This page says int32, because the Python in your browser is a 32-bit one, so its whole numbers are 4 bytes. Everything in this stage works the same with either; lesson 2.3 shows when the size matters.

The race

Here's the difference in speed, first as a picture:

A loop against np.sum. The same eight prices, added up two ways.45464544474648454546454447464845for p in prices: total += pnp.sum(prices)
1 of 5 The same eight prices, added up two ways.

Now for real. The notebook makes 50,000 numbers as a list and as an array, checks that a loop and np.sum agree (True), and then times each with %timeit from lesson 1.3. Run all four cells.

race.ipynb

On most computers np.sum is 30 to 100 times faster. The exact numbers depend on the machine, so yours won't match anyone else's, but the gap is always large. The more data, the more it matters: a pandas column with a million rows is a NumPy array, and every sum, mean and filter on it runs this way.

Wrong way: doubling a list

The same symbol can mean different things for a list and for an array. Run both cells.

double.ipynb

For a list, * 2 means "repeat the list twice": [1, 2, 1, 2]. For an array, it means "multiply every number by 2": [2, 4]. Neither is an error, so Python won't warn you if you use a list where you meant an array. When a result has twice as many items as you expected, check what kind of thing you had with type().

Check what you learned

arr = np.array([10, 20, 30]). What is arr + 5?
Why is np.sum(arr) faster than a for loop over a list?
[3, 4] * 3 is a list. What do you get?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis