Python Introduction to NumPy
Table of Contents + −
In the last lesson you learned Scheduling Python Scripts. So now your code can run on its own and do real work. Now we step into a new part of the course: working with data. And here is the first problem you hit. Say you have a million numbers and you want to multiply each one by two. With a plain Python list, that is slow and the code is clumsy. NumPy fixes both. It is the tool that makes Python fast at numbers, and almost every data and AI library is built on top of it.
🤔 Why do we need NumPy?
Python lists are great for everyday work. But they were never built for heavy math on lots of numbers. Two things go wrong when the data gets big.
- A Python list can hold anything: a number, a word, another list, all mixed together. That flexibility has a cost. Python has to check the type of every item while it works. On a million numbers, those tiny checks add up and the program slows down.
- Simple math feels clumsy. To double every number in a list you have to write a loop or a comprehension. There is no way to just say “multiply the whole thing by two”.
Here is the clumsy version with a plain list. You want to double each number, so you loop:
numbers = [1, 2, 3, 4, 5]doubled = []
for n in numbers: doubled.append(n * 2)
print(doubled)Output
[2, 4, 6, 8, 10]That works, but you wrote a whole loop just to double some numbers. Now imagine doing math like this across millions of rows. It gets slow and the code gets long. NumPy is the library that solves exactly this. It gives you a fast number container and lets you do math on the whole thing at once.
🐍 What is NumPy?
NumPy is short for “Numerical Python”. It is a third-party library that adds a new kind of container to Python called an array. An array is a grid of numbers, all of the same type, stored together in memory so the computer can run math on them very fast.
Think of it like an egg tray. Every slot holds the same kind of thing, lined up neatly in a row. Because everything is the same type and packed together, the computer does not have to stop and check each item. It just runs through them at full speed.
A few things make NumPy special:
- It stores numbers in a tight block of memory, not scattered around like a list. That is the main reason it is so fast.
- It lets you do math on a whole array in one line, with no loop. This is called vectorization, which just means the operation runs across every element for you.
- It is the foundation almost every data tool is built on. Pandas, scikit-learn, TensorFlow, and many others use NumPy arrays inside. So learning NumPy first makes all of those easier.
📦 Installing NumPy
NumPy does not come with Python by default, so you install it once with pip. Open your terminal and run this:
pip install numpyOnce it is installed, you bring it into your program with an import. Almost everyone imports it under the short name np, so your code is easy to read and matches what you will see everywhere online:
import numpy as np
print(np.__version__)Output
2.1.0The exact version number will depend on what you installed, so yours may differ. The point is that the import worked and NumPy is ready.
Tip
The name np is a convention, not a rule. You could write import numpy as numpy, but everyone uses np. Sticking to the common name makes your code easier for others to read.
💡 Your first NumPy array
Let’s make an array. You start from a normal Python list and hand it to np.array():
import numpy as np
prices = np.array([10, 20, 30, 40, 50])
print(prices)print(type(prices))Output
[10 20 30 40 50]<class 'numpy.ndarray'>Let’s read what happened:
np.array([10, 20, 30, 40, 50])takes a regular list and builds a NumPy array from it.- When you print the array, the numbers show up with spaces between them, not commas. That is just how NumPy displays an array.
- The type is
numpy.ndarray. That name stands for “N-dimensional array”, which is the official name for a NumPy array. So whenever you seendarray, it just means a NumPy array.
Now the part that makes it worth learning. Watch what happens when you do math on the whole array:
import numpy as np
prices = np.array([10, 20, 30, 40, 50])
print(prices * 2)print(prices + 5)Output
[ 20 40 60 80 100][15 25 35 45 55]See that? No loop. You wrote prices * 2 and NumPy doubled every number for you. Then prices + 5 added five to each one. This is vectorization in action. The math runs across the whole array at once, in one short line.
Caution
A plain Python list does not work this way. If you write [10, 20, 30] * 2 on a list, you do not get doubled numbers. You get the list repeated: [10, 20, 30, 10, 20, 30]. That is one big reason the NumPy array is a different thing.
🖥️ A more real example
Let’s make it feel real. Say Riya tracks the daily temperature for a week and wants the average, the hottest day, and the coldest day. With NumPy this is short and clear.
This script stores the readings in an array, then asks NumPy for the numbers Riya wants:
import numpy as np
temps = np.array([31, 33, 30, 35, 34, 29, 32])
print("Average:", temps.mean())print("Hottest:", temps.max())print("Coldest:", temps.min())print("Total readings:", temps.size)Output
Average: 32.0Hottest: 35Coldest: 29Total readings: 7Here is what each part is doing:
temps.mean()adds up all the numbers and divides by how many there are, giving the average in one call.temps.max()andtemps.min()find the biggest and smallest values for you.temps.sizetells you how many numbers are in the array.
Notice these are built right into the array. You do not import anything extra or write any math by hand. With a plain list you would have to call sum() and len() yourself and divide, and there is no .mean() at all.
🌍 Where NumPy is used
NumPy is quiet but everywhere. Most of the time you are using it without even knowing, because other tools sit on top of it. Here are the common places it shows up.
| Field | How NumPy helps |
|---|---|
| Data analysis | Pandas, the main data tool, stores its columns as NumPy arrays inside |
| Machine learning | scikit-learn feeds data to models as NumPy arrays |
| Deep learning | Tools like TensorFlow and PyTorch use NumPy-style arrays for their math |
| Images | A photo is just a grid of numbers, so it loads naturally as a NumPy array |
| Science and finance | Fast math on large tables of numbers, like prices or sensor readings |
⚠️ Common Mistakes
A few small things trip up almost everyone at the start:
- Forgetting to install NumPy. If you skip
pip install numpy, the import fails withModuleNotFoundError. Install it once, then it is there for that environment.
# ❌ This fails if NumPy was never installedimport numpy as np # ModuleNotFoundError: No module named 'numpy'
# ✅ Fix: run this in the terminal first, then the import works# pip install numpy- Expecting a list to behave like an array. Math on a list does not work element by element. You must turn it into an array first.
nums = [1, 2, 3]
# ❌ This repeats the list, it does not double the numbersprint(nums * 2) # [1, 2, 3, 1, 2, 3]
# ✅ Make it an array first, then the math runs on each numberimport numpy as npprint(np.array(nums) * 2) # [2 4 6]- Mixing types in one array. An array is meant to hold one type. If you mix numbers and text, NumPy quietly turns everything into text, and your math breaks. Keep one array to one kind of data.
✅ Best Practices
Small habits that keep your NumPy code clean:
- Always import it as
np. It is what the whole community uses, so your code stays easy to read. - Use an array when you have lots of numbers and want to do math on them. For a short list of mixed things, a plain list is still fine.
- Let NumPy do the looping. If you find yourself writing a
forloop to do math on every element, there is almost always a one-line vectorized way instead. - Keep one array to one type of number, so the fast math keeps working.
🧩 What You’ve Learned
✅ NumPy is a library that adds a fast number container called an array (an ndarray) to Python.
✅ It is faster than a plain list because it stores numbers of the same type packed together in memory.
✅ Vectorization lets you do math on a whole array in one line, with no loop, like prices * 2.
✅ Arrays come with handy built-in tools like .mean(), .max(), .min(), and .size.
✅ NumPy is the foundation that Pandas, scikit-learn, and most data and AI tools are built on.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
Why is a NumPy array faster than a plain Python list for math on many numbers?
Why: An array holds one type packed in memory, so NumPy runs through the numbers without stopping to check each item's type the way a mixed-type list forces it to.
- 2
What does the term 'vectorization' mean in NumPy?
Why: Vectorization means the operation runs across every element for you, so prices * 2 doubles the whole array in one line without a loop.
- 3
What do you get if you write [10, 20, 30] * 2 on a plain Python list?
Why: On a list, * 2 repeats the list. To double each number you need a NumPy array, where prices * 2 works element by element.
- 4
What is the standard way to import NumPy?
Why: Almost everyone writes import numpy as np. It is a convention that keeps code short and consistent with examples everywhere.
🚀 What’s Next?
You now know what NumPy is and why it is fast. Next we look closely at the array itself: how to build arrays in different ways, check their shape, and pick out the values you want.