Chapter 4: NumPy Getting Started
NumPy – Getting Started (Really from Zero)
What is NumPy in one honest sentence?
NumPy is the library that lets Python handle lots of numbers very quickly and lets you do mathematics on entire collections of numbers at once — without writing slow for-loops.
It is the foundation of almost everything serious in:
- Data analysis (pandas)
- Machine learning & deep learning
- Scientific computing
- Image processing
- Finance
- Signal processing
- Statistics
- Simulations
Most important mindset change you need today:
Normal Python lists → think one number at a time NumPy arrays → think whole group of numbers at once
This single change makes your code:
- 10–100× faster
- Much shorter
- Much easier to read (once you get used to it)
Step 1 – Let’s Install & Import (You Probably Already Have It)
In most modern environments (Jupyter, Colab, Anaconda, recent Python installations) NumPy is already there.
But if you need to install it:
|
0 1 2 3 4 5 6 |
pip install numpy |
Now in your Python code / notebook:
|
0 1 2 3 4 5 6 |
import numpy as np |
Convention everyone follows: We almost always write np — not numpy, not num, not npy. Just accept it — it’s like pd for pandas, plt for matplotlib.
|
0 1 2 3 4 5 6 7 |
print(np.__version__) # good to know which version you have # example output: 1.26.4 or 2.0.0 or 2.1.1 etc. |
Step 2 – Your Very First NumPy Array
Let’s create one right now.
|
0 1 2 3 4 5 6 7 8 9 |
# Classic first array scores = np.array([78, 92, 65, 84, 71, 88, 95]) print(scores) # [78 92 65 84 71 88 95] |
Compare with normal Python list:
|
0 1 2 3 4 5 6 7 8 |
python_list = [78, 92, 65, 84, 71, 88, 95] print(python_list) # [78, 92, 65, 84, 71, 88, 95] |
They look similar — but they are very different inside the computer.
Step 3 – First Magic: Math on the whole array (No loops!)
Let’s give everyone 5 bonus points.
Classic Python (slow when list is big):
|
0 1 2 3 4 5 6 7 8 9 |
new_scores = [] for score in python_list: new_scores.append(score + 5) print(new_scores) |
NumPy (fast & beautiful):
|
0 1 2 3 4 5 6 7 8 |
bonus = scores + 5 print(bonus) # [83 97 70 89 76 93 100] |
No for loop. No list comprehension. NumPy did the +5 for every element automatically.
This is called vectorization — the #1 reason people fall in love with NumPy.
Let’s try more operations:
|
0 1 2 3 4 5 6 7 8 9 10 |
print(scores * 2) # double everyone's score print(scores / 10) # convert to scale 0–10 print(scores ** 2) # square print(np.sqrt(scores)) # square root print(scores > 80) # who passed 80? → boolean array |
Output examples:
|
0 1 2 3 4 5 6 7 8 |
[156 184 130 168 142 176 190] [7.8 9.2 6.5 8.4 7.1 8.8 9.5] [False True False True False True True] |
Step 4 – Most Common Ways to Create Arrays (You’ll use these daily)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 1. From Python list (most common at beginning) temperatures = np.array([23.4, 25.1, 19.8, 28.7, 22.0]) # 2. All zeros (very useful to prepare space) zeros = np.zeros(8) # 1D zeros2d = np.zeros((4, 6)) # 4 rows × 6 columns # 3. All ones ones = np.ones((3, 5)) # 4. Fill with the same value budget = np.full((12,), 5000.0) # 12 months × 5000 budget # 5. Sequence of numbers (very very common) a = np.arange(0, 20, 2) # 0, 2, 4, ..., 18 b = np.linspace(0, 1, 11) # 0.0, 0.1, 0.2, ..., 1.0 # 6. Random numbers – extremely important np.random.seed(42) # ← makes results repeatable (very useful!) uniform = np.random.rand(6) # 6 numbers between 0.0 and 1.0 normal = np.random.randn(10) # 10 numbers ~ Normal(0,1) dice = np.random.randint(1, 7, size=20) # 20 dice rolls |
Quick tip students always ask:
|
0 1 2 3 4 5 6 7 |
arange(0, 10, 0.5) → sometimes misses last value due to float precision → better: np.arange(0, 10.01, 0.5) or np.linspace(0, 10, 21) |
Step 5 – The 4 Things You Should Always Look At
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
data = np.random.randint(0, 100, size=(3, 5)) print(data) # example: # [[83 45 67 91 12] # [39 73 8 29 64] # [19 52 37 88 15]] print(data.shape) # (3, 5) ← MOST IMPORTANT print(data.ndim) # 2 print(data.size) # 15 print(data.dtype) # int64 |
Real examples you will see soon:
|
0 1 2 3 4 5 6 7 8 |
(10000, 784) → 10,000 images, each flattened to 784 pixels (28×28) (32, 224, 224, 3) → batch of 32 color images 224×224 pixels (5000,) → 5000 time points of stock price |
Step 6 – Very Important Warning: Copying Arrays
|
0 1 2 3 4 5 6 7 8 9 10 |
a = np.array([10, 20, 30, 40]) b = a # ← THIS IS NOT A COPY! b[0] = 999 print(a) # [999 20 30 40] ← a changed too! |
Correct ways to copy:
|
0 1 2 3 4 5 6 7 8 |
c = a.copy() # clearest & safest d = np.copy(a) e = a[:] # usually safe, but can be view in some cases |
Golden rule to remember:
|
0 1 2 3 4 5 6 7 |
b = a → same data, two names b = a.copy() → new independent copy |
Step 7 – Your First Tiny Realistic Example
Let’s say you have exam percentages and you want to:
- Add 3 bonus points
- Convert to scale 0–10
- See who got > 85%
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
marks = np.array([78, 92, 65, 88, 71, 95, 82]) # Operations adjusted = marks + 3 scale_10 = adjusted / 10 excellent = adjusted > 85 print("Adjusted:", adjusted) print("Scale /10 :", np.round(scale_10, 1)) print("Excellent:", excellent) print("Who passed with distinction:", marks[excellent]) |
Output:
|
0 1 2 3 4 5 6 7 8 9 |
Adjusted: [81 95 68 91 74 98 85] Scale /10 : [8.1 9.5 6.8 9.1 7.4 9.8 8.5] Excellent: [False True False True False True False] Who passed with distinction: [92 88 95] |
No loops — clean, fast, readable.
Your First NumPy Survival Checklist
You should now be able to:
- Import NumPy (import numpy as np)
- Create arrays from lists
- Use zeros, ones, arange, linspace
- Generate random numbers
- Do math on whole arrays (+ − × ÷ ** sqrt round …)
- Check .shape, .dtype, .ndim, .size
- Understand why we avoid for loops
- Copy arrays correctly with .copy()
Where do you want to go next?
Pick whatever feels most useful right now:
- More ways to create arrays (realistic examples)
- Indexing & slicing (very important next step)
- Boolean masks — selecting data with conditions
- Broadcasting — the magic shape matching
- First statistics (mean, median, std, min, max…)
- Reshaping arrays (very common in data & ML)
- Common beginner mistakes and how to avoid them
- A small realistic mini-project together
Just tell me a number or say what you feel you need most — we continue exactly from here.
You’re doing great — let’s keep going! 😊
