Goal:
By the end of this lesson, you’ll:
- Understand what NumPy is and why it’s fast
- Work with arrays instead of lists
- Perform fast mathematical operations
- Manipulate data like a data scientist
What Is NumPy?
NumPy (Numerical Python) is a powerful library used for:
- Fast calculations with large datasets
- Handling numerical matrices (tables of numbers)
- Vectorized operations (no more for-loops!)
First, install it:
pip install numpy
And import it:
import numpy as np
Creating Arrays
arr = np.array([1, 2, 3, 4])
print(arr) # [1 2 3 4]
print(arr.shape) # (4,)
2D array:
matrix = np.array([[1, 2], [3, 4]])
print(matrix)
Generate useful arrays:
np.zeros(5) # [0. 0. 0. 0. 0.]
np.ones((2, 3)) # 2 rows, 3 cols of ones
np.arange(0, 10, 2) # [0 2 4 6 8]
np.linspace(0, 1, 5) # 5 evenly spaced values from 0 to 1
Array Math (Vectorized)
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
print(a + b) # [11 22 33]
print(a * b) # [10 40 90]
print(a ** 2) # [1 4 9]
✅ Way faster than using Python for loops!
Reshaping and Indexing
x = np.arange(12) # [0 to 11]
x = x.reshape(3, 4) # 3 rows, 4 columns
print(x)
print(x[0, 1]) # value at row 0, column 1
print(x[:, 2]) # all rows, column 2
Basic Statistics
data = np.array([10, 20, 30, 40, 50])
print(data.mean()) # 30.0
print(data.std()) # Standard deviation
print(data.max()) # 50
print(data.min()) # 10
🧪 6. Practice Time
Try these in your blog_env notebook:
- Create an array of daily sales: [2500, 2700, 2600, 3000, 3100]
- Calculate total, average, and standard deviation
- Create a 2D array of stock levels by product and day
- Reshape a 1D array of 12 numbers into 3 rows and 4 columns
- Use slicing to get the second column of that matrix
✅ Summary
- NumPy is fast, memory-efficient, and the foundation of data science
- Arrays replace lists for calculations
- Use .mean(), .std(), and .reshape() often
- Avoid loops — vectorize your code!


Leave a comment