By the end of this lab, you will be able to:
Understand what NumPy is and why it is useful.
Create and manipulate arrays of various shapes and data types.
Use indexing and slicing to access and modify array data.
Apply mathematical operations efficiently using NumPy.
Understand and use array reshaping and broadcasting.
Complete structured exercises to reinforce your learning.
NumPy (short for Numerical Python) is a library that helps you work with arrays and perform fast mathematical operations. It is widely used in:
Data science
Artificial intelligence
Machine learning
Scientific computing
Python lists are great for storing collections of items, but they are:
Slower for numerical operations
Less flexible when handling multi-dimensional data (like grids or matrices)
NumPy arrays:
Are faster and more efficient
Let you apply mathematical operations to entire datasets at once (called vectorisation)
Support multi-dimensional structures like 2D matrices and 3D tensors
Think of a NumPy array like a spreadsheet of numbers. Instead of going cell by cell (like with a Python loop), NumPy lets you do calculations on the whole sheet at once.
Before we can use NumPy, we need to install and import it.
If you're using Google Colab or Jupyter notebooks, NumPy might already be installed. If not:
pip install numpy
Import NumPy using the common alias np:
import numpy as np
Now any time you want to use NumPy’s features, you’ll write np.function_name().
Let’s begin by creating some basic NumPy arrays. Think of an array as a row or grid of numbers.
NumPy offers many ways to generate arrays without manually typing values:
Once you have arrays, you need to know how to access parts of them. This is called indexing and slicing.
NumPy makes math easy by applying operations to entire arrays.
These functions apply to each element without writing a loop!
You can change an array’s shape using .reshape():
Broadcasting allows arrays with different shapes to still operate together:
Broadcasting saves time by eliminating the need for nested loops.
Try the following problems to test your understanding. Discuss with a classmate or ask your teacher if stuck.
Create a 5x5 array filled with zeros.
Make a 3x3 identity matrix.
Create a 1D array containing the numbers from 10 to 50 inclusive.
Use this array: arr = np.array([5, 10, 15, 20, 25, 30])
Slice the second to fourth elements.
Slice every second element.
Create a 3x3 array (your own values) and extract:
The first column.
The last row.
Create two 1D arrays with three numbers each. Perform:
Addition
Subtraction
Multiplication
Make an array with values from 1 to 100. Calculate:
Mean
Maximum
Standard Deviation (np.std())
Create a 1D array with 12 elements. Reshape it into 3 rows and 4 columns.
Multiply all elements in a 3x3 matrix by 5.
Add the vector [1, 2, 3] to each row of a 3x3 matrix.
If you finish early, try this:
Create a 10x10 matrix of random numbers between 0 and 1.
Replace all numbers greater than 0.5 with 1, and the rest with 0.
Count how many 1s and 0s are in the matrix using np.sum().