NumPy Arrays vs Python Lists
A NumPy array is like a Python list, but built for fast maths on lots of numbers at once.
What you will learn
- Create a NumPy array
- Explain why arrays beat lists for numbers
- Read an array’s shape
Why not just use a list?
You already know Python lists. They are great, but slow and clumsy when you have thousands of numbers to crunch. NumPy (short for “Numerical Python” — a free add-on library for fast number work) gives you the array — a list-like container built only for numbers, and it is much faster.
NumPy is the foundation: Pandas, Matplotlib and scikit-learn are all built on top of it. Learn it once and it pays off everywhere.
Your first array
You create an array from a list with np.array(...). By convention everyone imports NumPy as np.
import numpy as np
prices = np.array([60, 80, 120, 140])
print(prices)
print(type(prices))
print('shape:', prices.shape)Note: Output: [ 60 80 120 140] <class 'numpy.ndarray'> shape: (4,) The array holds four numbers. Its type is ndarray (n-dimensional array). The shape (4,) means “4 items in one row”.
The big difference: maths on the whole array
With a list, multiplying by 2 just repeats it. With an array, it does real maths on every number at once. Watch the contrast:
my_list = [60, 80, 120, 140]
my_array = np.array([60, 80, 120, 140])
print(my_list * 2) # a list just repeats itself
print(my_array * 2) # an array doubles every numberNote: Output: [60, 80, 120, 140, 60, 80, 120, 140] [120 160 240 280] The list got longer (repeated). The array doubled each value — exactly what you want for maths. This is the whole reason arrays exist.
| Python list | NumPy array | |
|---|---|---|
| Best for | Mixed items (text, numbers) | Lots of numbers |
| Maths on all items | Manual loop needed | Built-in & instant |
| Speed on big data | Slow | Very fast |
| Used by | General Python | Pandas, ML, charts |
Tip: Rule of thumb: a bag of mixed things → use a list. A column of numbers you will do maths on → use a NumPy array (or a Pandas column, which is an array underneath).
Q. What does np.array([1, 2, 3]) * 2 produce?
✍️ Practice
- Make an array of five test scores and print its
.shape. - Create a list and an array of
[10, 20, 30], multiply each by 3, and compare the two outputs.
🏠 Homework
- In two or three sentences, explain to a classmate why NumPy arrays are better than lists for working with thousands of numbers.