NumPy (Numerical Python) is the foundational library for numerical computing in Python. It introduces the array — a data structure that is far faster and more memory-efficient than a regular Python list for numeric operations, and is the backbone that Pandas itself is built on.
| Feature | Python List | NumPy Array |
|---|---|---|
| Speed | Slower for large data | Much faster (vectorized operations) |
| Math operations | Requires loops | Direct: array + 5 adds to every element |
| Memory usage | Higher | Lower (fixed data type) |
| Multi-dimensional data | Awkward (nested lists) | Native support (2D, 3D+ arrays) |
pip install numpy
import numpy as npimport numpy as np
marks = np.array([78, 85, 92, 66, 88])
print(marks)
print(type(marks))
matrix = np.array([[1,2,3],[4,5,6]]) # 2D array
print(matrix)
zeros = np.zeros(5) # array of 5 zeros
ones = np.ones((2,3)) # 2x3 array of ones
seq = np.arange(0, 10, 2) # like range(), but returns an arraymarks = np.array([78, 85, 92, 66, 88])
print(marks[0]) # first element
print(marks[-1]) # last element
matrix = np.array([[1,2,3],[4,5,6]])
print(matrix[0, 2]) # row 0, column 2 -> 3
print(matrix[1, :]) # entire second rowmarks = np.array([78, 85, 92, 66, 88, 95])
print(marks[1:4]) # index 1 to 3
print(marks[:3]) # first 3 elements
print(marks[::2]) # every 2nd elementBroadcasting lets NumPy apply an operation to every element of an array automatically, without writing a loop — this is one of NumPy's most powerful features.
marks = np.array([70, 80, 90])
bonus_marks = marks + 5 # adds 5 to EVERY element automatically
print(bonus_marks)
scaled = marks * 1.1 # 10% boost to every element
print(scaled)a = np.array([10, 20, 30])
b = np.array([1, 2, 3])
print(a + b) # [11 22 33]
print(a - b) # [9 18 27]
print(a * b) # [10 40 90]
print(a / b) # [10. 10. 10.]| Function | Purpose |
|---|---|
| np.sum(arr) | Total of all elements |
| np.mean(arr) | Average value |
| np.max(arr) / np.min(arr) | Largest / smallest value |
| np.std(arr) | Standard deviation |
| np.median(arr) | Median value |
marks = np.array([78, 85, 92, 66, 88])
print("Mean:", np.mean(marks))
print("Max:", np.max(marks))
print("Std Dev:", round(np.std(marks), 2))reshape() lets you change an array's dimensions without changing its data — very useful when preparing data for analysis or ML models.
data = np.arange(1, 13) # 1D array: 1 to 12
reshaped = data.reshape(3, 4) # 3 rows, 4 columns
print(reshaped)Basic (5 Questions)
1. Create a NumPy array of 6 exam marks and print its type.
2. Create a 2x3 array of zeros and a 3x2 array of ones.
3. Print the first and last element of an array using indexing.
4. Slice an array to get only the middle 3 elements.
5. Use np.arange() to create an array of even numbers from 0 to 20.
Intermediate (5 Questions)
1. Use broadcasting to add 10 bonus marks to an array of student scores.
2. Calculate the mean, max, min, and standard deviation of a marks array.
3. Create a 2D array (3x3) and access a specific row and a specific element.
4. Reshape a 1D array of 9 elements into a 3x3 grid.
5. Multiply two arrays element-wise and print the result.
Advanced (5 Questions)
1. Build a 'Grade Booster' that adds different bonus points to different subjects using broadcasting on a 2D array.
2. Given a 2D array of student marks (rows = students, columns = subjects), calculate each student's average using aggregation along an axis.
3. Reshape a dataset of 24 sales figures into a 4x6 grid representing 4 weeks x 6 days, then find the weekly totals.
4. Compare the execution speed of summing 1 million numbers using a Python loop vs. np.sum() (using the time module).
5. Build a simple 'Data Normalizer' that scales an array of marks to a 0-1 range using broadcasting: (x - min) / (max - min).
Q1. What is the main advantage of NumPy arrays over lists?
Q2. Which function creates an array of evenly spaced values like range()?
Q3. What does broadcasting allow?
Q4. Which function reshapes an array?
Q5. Which function calculates the average of an array?
Assignment: Student Marks Analyzer with NumPy
Expected Output: A script that computes per-student and per-subject averages from a 2D marks array, correctly applying grace marks using broadcasting.
Use this space to write down key points, doubts, and your own examples from today's session.