Session 17 ยท Phase 3: Python

NumPy Essentials

NumPy is the numerical engine of the Python data stack โ€” fast arrays, vectorized operations, and the foundation Pandas is built on.

โฑ ~1.5 hrs ๐Ÿ“š Core content ๐ŸŽฏ High priority

Learning Objectives

1. What NumPy Is & Why It Matters

NumPy (Numerical Python) provides fast, multi-dimensional arrays and a large library of mathematical functions. It's the foundation that Pandas, Matplotlib, and most of the data stack are built on.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)   # [1 2 3 4 5]

2. Creating Arrays

import numpy as np

# From a Python list
arr = np.array([1, 2, 3, 4, 5])

# Arrays of zeros or ones
zeros = np.zeros(5)          # [0. 0. 0. 0. 0.]
ones  = np.ones((2, 3))      # 2x3 array of 1s

# A range (like Python's range, but returns an array)
r = np.arange(0, 10, 2)      # [0 2 4 6 8]

# 2-D array (matrix-like)
m = np.array([[1, 2, 3], [4, 5, 6]])

3. Array Attributes

Every array has attributes describing its structure โ€” a common interview question.

m = np.array([[1, 2, 3], [4, 5, 6]])

m.shape    # (2, 3)  โ€” 2 rows, 3 columns
m.ndim     # 2       โ€” number of dimensions
m.size     # 6       โ€” total elements
m.dtype    # dtype('int64') โ€” data type of elements
AttributeMeaning
shapedimensions (rows, columns, โ€ฆ)
ndimnumber of dimensions
sizetotal number of elements
dtypedata type of the elements

4. Indexing and Slicing

Indexing gets a single element; slicing gets a range of elements.

arr = np.array([10, 20, 30, 40, 50])

arr[0]      # 10     (indexing โ€” first element)
arr[-1]     # 50     (last element)
arr[1:4]    # [20 30 40]  (slicing โ€” elements 1 to 3)

# 2-D indexing
m = np.array([[1, 2, 3], [4, 5, 6]])
m[1, 2]     # 6  (row 1, column 2)
๐Ÿ“
Remember: Python is 0-indexed, and slices are half-open โ€” arr[1:4] includes index 1, 2, 3 but not 4.

5. Vectorization โ€” the Whole Point

Vectorization applies an operation to an entire array at once, instead of looping over each element. This is why NumPy is dramatically faster than pure Python loops.

arr = np.array([1, 2, 3, 4, 5])

arr * 2      # [2 4 6 8 10]   โ€” no loop needed
arr + 10     # [11 12 13 14 15]
arr ** 2     # [1 4 9 16 25]

# Two arrays, element-wise
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
a + b        # [11 22 33]
๐ŸŒ
Real World: "Multiply every sales value by 1.18 to add GST" is one line in NumPy โ€” sales * 1.18. With plain Python you'd write a loop over thousands of values.

6. Broadcasting

Broadcasting lets NumPy operate on arrays of different shapes by stretching the smaller one โ€” the simplest case is adding a scalar to every element.

arr = np.array([1, 2, 3])

arr + 100    # [101 102 103]  โ€” the scalar 100 "broadcasts" to every element

# Column (3x1) + row (1x3) โ†’ broadcasts to a 3x3
col = np.array([[1], [2], [3]])
row = np.array([[10, 20, 30]])
col + row
# [[11 21 31]
#  [12 22 32]
#  [13 23 33]]
๐Ÿ’ก
Pro Tip: Broadcasting works when shapes are "compatible" โ€” either equal, or one of them is 1. If you get a shape-mismatch error, that's broadcasting telling you the shapes don't align.

7. Common Functions

arr = np.array([1, 2, 3, 4, 5])

np.mean(arr)     # 3.0
np.median(arr)   # 3.0
np.std(arr)      # standard deviation
np.min(arr)      # 1
np.max(arr)      # 5
np.sum(arr)      # 15

# Reshape
m = np.arange(6).reshape(2, 3)   # [[0 1 2], [3 4 5]]

8. NumPy Array vs Python List

Python listNumPy array
Element typesmixed allowedsingle fixed dtype
Speedslower (loops)fast (vectorized)
Memoryhighercompact
Math opselement-by-element loopswhole-array at once
๐Ÿ“‹ Stable content โ€” Reviewed: August 2026

9. Interview Questions (with Model Answers)

The NumPy questions interviewers ask. Self-test before revealing.

IQ1. What is NumPy, and why is it used in data analysis?

Model answer: "NumPy is a library for numerical computing. It provides fast, multi-dimensional arrays and mathematical functions, and it's the foundation that Pandas and Matplotlib build on."

IQ2. What is an ndarray, and what are its main attributes?

Model answer: "An ndarray is NumPy's n-dimensional array. Its key attributes are shape (dimensions), ndim (number of dimensions), size (total elements), and dtype (element data type)."

IQ3. What's the difference between indexing and slicing?

Model answer: "Indexing returns a single element by position; slicing returns a range of elements. Slices are half-open โ€” arr[1:4] gives elements 1, 2, and 3."

IQ4. What is vectorization, and why is it faster than loops?

Model answer: "Vectorization applies an operation to a whole array at once using optimized C code under the hood, instead of looping element-by-element in Python. That's why it's dramatically faster."

IQ5. What is broadcasting in NumPy?

Model answer: "Broadcasting lets NumPy operate on arrays of different shapes by stretching the smaller one. The simplest example is adding a scalar to every element of an array."

IQ6. What's the difference between a NumPy array and a Python list?

Model answer: "A list can hold mixed types and is slower; a NumPy array holds a single fixed dtype, uses less memory, and supports fast vectorized operations โ€” which is what data work needs."

IQ7. How do you reshape an array?

Model answer: "With .reshape() โ€” for example, np.arange(6).reshape(2, 3) turns a 1-D array of 6 into a 2ร—3 matrix, as long as the total size matches."

IQ8. What statistical functions does NumPy provide?

Model answer: "The common ones โ€” mean, median, std, min, max, sum, and percentile โ€” all operate on the whole array at once, which is much faster than manual calculation."

IQ9. Why is NumPy faster than pure Python for numerical work?

Model answer: "Because NumPy is vectorized โ€” operations run in optimized compiled C over contiguous memory, avoiding Python's per-element interpreter overhead and loops."

Hands-On Project: NumPy in Action

Write NumPy code for each task. Run it and check the output.

Steps

  1. Create a NumPy array of daily sales: [120, 150, 90, 200, 180].
  2. Print its shape, size, and dtype.
  3. Compute the total, mean, and max sales using vectorized functions.
  4. Add 18% tax to every value (vectorized, no loop).
  5. Select the first three days using slicing.
  6. Reshape a 1-D array of 6 values into a 2ร—3 matrix.
View Solution / Walkthrough
import numpy as np

# 1. Daily sales
sales = np.array([120, 150, 90, 200, 180])

# 2. Attributes
print(sales.shape)   # (5,)
print(sales.size)    # 5
print(sales.dtype)   # int64

# 3. Aggregate functions
print(sales.sum())   # 740
print(sales.mean())  # 148.0
print(sales.max())   # 200

# 4. Add 18% tax (vectorized)
with_tax = sales * 1.18   # no loop

# 5. First three days
first3 = sales[0:3]       # [120 150 90]

# 6. Reshape 6 values into 2x3
m = np.arange(6).reshape(2, 3)   # [[0 1 2], [3 4 5]]

Key Takeaways

1

NumPy is the fast array engine that Pandas is built on.

2

shape, ndim, size, and dtype describe an array.

3

Vectorization = whole-array operations, no loops โ€” that's the speed.

4

Broadcasting stretches smaller arrays to match larger ones.

5

Indexing = one element; slicing = a range (half-open).

Objective Questions โ€” Test Your Understanding

Q1. What is the primary data structure in NumPy?

Q2. What does "vectorization" mean in NumPy?

Q3. Which array attribute gives its dimensions (rows, columns)?

Q4. What is "broadcasting" in NumPy?

Q5. Which NumPy function creates an array filled with zeros?