NumPy Arrays
A NumPy array is a row of values of one type. It holds numbers in a fixed order, like a list with maths built in.
An array from a list
Three prices become one array. The array prints in square brackets.
import numpy as np
prices = np.array([4.5, 6.0, 12.0])
print(prices)
Counting with arange
arange builds numbers from a start to a stop. The stop is not included.
import numpy as np
ids = np.arange(101, 106)
print(ids)
The type of an array
dtype names the one type of the values. These prices are floats.
import numpy as np
prices = np.array([4.5, 6.0, 12.0])
print(prices.dtype)
Example
import io
import numpy as np
import pandas as pd
SALES_CSV = """
order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""
sales = pd.read_csv(io.StringIO(SALES_CSV))
units = np.array(list(sales["units"]))
print(units)
print(units.sum())
print(units.mean())
Key points
- NumPy is a library for fast maths on rows of numbers.
- Use np.array to build an array from a list.
- Use np.arange to build an array of numbers in steps.
- An array has one type for every value.
- Use len to count the values in an array.
In this example, the sales table loads from its CSV text. The units column becomes a list, and np.array turns that list into an array. The array prints with all twelve order sizes. sum adds them to 28. mean divides that total by twelve.
Note
An array with mixed numbers becomes one type. A list with 1 and 2.5 becomes floats.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
Which call builds an array from the list [1, 2, 3]?