The main chapters of this book teach by guided invention: you derive
each algorithm yourself, one small step at a time, before the concept is
named. These 47 exercises are a different mode — drill
practice. Each notebook gives you one concrete formula or
algorithm and asks you to implement it in Python from scratch. There is
no arc or narrative; every notebook stands alone.
When to use these. Work through the five groups in
order — Expressions, If/Else, Loops, Functions, Recursion — as you
finish the corresponding LBI chapter, or as warm-ups before the ML
chapters. The ML themes (gradient descent, entropy, neural networks)
preview concepts you will invent in full later; if a problem feels too
hard, come back after the relevant chapter.
How each notebook works. Read the problem statement.
Fill in the starter cell wherever you see # TODO. Run the
test cell at the bottom — if it prints All tests passed!,
you’re done.
01 — Expressions
Best done after: Chapter 3 (Expressions & Functions)
Each problem is a single Python expression. No loops, no conditionals
— just arithmetic, math.exp, and one or two variables.
#
Difficulty
Concept
Open in Colab
1
Easy
Mean of three numbers
2
Easy
Z-score for one value
3
Easy
Min-max scale one value
4
Medium
Euclidean distance (2-D)
5
Medium
Probability of two independent events
6
Medium
Squared error for one prediction
7
Medium
Sigmoid activation function
8
Hard
One term of a covariance sum
9
Hard
One gradient descent update step
02 — If / Else
Best done after: Chapter 4 (If/Else)
Each problem requires a conditional choice — "A" vs
"B", "pure" vs "mixed",
"train" vs "val" vs "test".
Threshold-based decisions are everywhere in ML.
#
Difficulty
Concept
Open in Colab
1
Easy
Flag a single outlier by z-score
2
Easy
Classify a probability as likely or unlikely
3
Easy
Turn a sigmoid output into a class label
4
Medium
Classify one prediction as TP / FP / TN / FN
5
Medium
Assign a point to its nearest centroid
6
Medium
Classify correlation strength and direction
7
Medium
Min-max normalize without dividing by zero
8
Hard
Assign a sample to train / val / test by position
9
Hard
Decide whether a tree node is pure
03 — Loops
Best done after: Chapter 8 (Loops & Arrays)
Each problem asks you to accumulate a result by looping over a list.
The themes run from basic statistics (mean, std dev) up to gradient
descent and entropy — you will invent the full versions of those ideas
in Chapters 15 and 21.
#
Difficulty
Concept
Open in Colab
1
Easy
Compute the mean with a loop
2
Easy
Sum of squared deviations
3
Medium
Compute the standard deviation
4
Medium
Find all outliers in a dataset
5
Medium
Min-max normalize an entire list
6
Medium
Distances from a query point to a training set
7
Hard
Build a confusion matrix over a dataset
8
Hard
Compute covariance between two lists
9
Hard
Run several iterations of gradient descent
10
Hard
Compute the entropy of a list of labels
04 — Functions
Best done after: Chapter 3 (Expressions & Functions) and
Chapter 8 (Loops & Arrays)
These problems ask you to compose smaller functions into larger ones
— the same building-block pattern the book uses throughout. The capstone
notebooks (regression pipeline, entropy + information gain, sigmoid +
perceptron) are mini-versions of full LBI chapters.
#
Difficulty
Concept
Open in Colab
1
Easy
mean() function
2
Easy
variance() using mean()
3
Easy
std_dev() using variance()
4
Medium
find_outliers() from stats functions
5
Medium
Simple k-nearest-neighbors classifier
6
Medium
precision(), recall(), and
f1_score()
7
Medium
correlation() from covariance and std dev
8
Hard
Linear regression pipeline (predict, mse,
gradient_descent_fit)
9
Hard
entropy() and information_gain()
10
Hard
sigmoid() and perceptron()
05 — Recursion
Best done after: Chapter 5 (Recursion)
Each problem has a natural recursive structure: a base case and a
case that reduces to a smaller version of itself. The ML themes
(learning-rate decay, tree depth) appear in the Gradient Descent and
Decision Tree chapters.