The book uses code as a teaching means in a tutorial-like fashion. After a little bit of opening theory, all of the source code for each project is explained step-by-step and in detail.
One of the smallest possible programming languages is used as an example to understand ideas about Turing Machines and interpreters.
An interpreter for Nano BASIC, a dialect of Tiny BASIC—a programming language that was used during the personal computer revolution.
A program that converts modern photos for display on a classic 1980s Mac by performing 1-bit (black & white) Atkinson dithering and converting them to MacPaint format.
Impressionist uses a simple stochastic hill climbing algorithm to create abstract art from photographs that (sometimes) looks like the work of an impressionist painter.
An implementation of the CHIP-8 virtual machine that can play all of the 1970s games that were released for CHIP-8.
The simplest possible NES emulator with support for playing real games but lacking sound, performance, compatibility with most mappers, and several other features.
You draw a digit and the program can interpret your drawing (KNN classification) and/or complete it (KNN regression).
Acknowledgments
xv
Introduction
xvii
Chapter 1: The Smallest Possible Programming Language
3
Chapter 2: Writing a BASIC Interpreter
21
Chapter 3: Retro Image Processing
63
Chapter 4: A Stochastic Painting Algorithm
89
Chapter 5: Building a CHIP-8 Virtual Machine
115
Chapter 6: Emulating the NES Game Console
141
Chapter 7: Classification with K-Nearest Neighbors
205
Chapter 8: Regression with K-Nearest Neighbors
221
Afterword
231
Appendix: Bitwise Operations
237
Index
245
If you’ve been programming for a while, you may have found yourself wondering about the deeper principles behind the code. How are programming languages implemented? What does an interpreter really do? How does the microprocessor execute instructions at a fundamental level? How does a machine learning algorithm make decisions?
Computer Science from Scratch is for experienced Python programmers who want to fill in those gaps—not through abstract lectures, but through carefully designed projects that bring core CS concepts to life. Understanding these fundamental building blocks will make you a more versatile and effective programmer.
Each chapter presents a focused, hands-on project that teaches a fundamental idea in computer science:
INTERPRETERS: Understand syntax, parsing, and evaluation by writing a BASIC interpreter
EMULATORS: Learn computer architecture by building an NES emulator from the ground up
GRAPHICS: Explore image manipulation and algorithmic art through computer graphics projects
MACHINE LEARNING: Demystify classification by implementing a simple, readable KNN model
These projects aren’t about building tools—they’re structured lessons that use code to reveal how computing works. Each chapter concludes with real-world context, thoughtful extensions, and exercises to deepen your understanding.
Authored by David Kopec, a computer science professor and author of the popular Classic Computer Science Problems series, this is not a beginner’s book, and it’s not a theory-heavy academic text. It’s a practical, code-driven introduction to the essential ideas and mechanisms of computer science—written for programmers who want more than syntax.
If you’ve been writing Python and are ready to explore the foundations behind computing, this book will guide you there—with clarity, depth, and purpose.
While all of the code is in the book alongside detailed step by step explanations, many readers prefer to have it open in an IDE side-by-side as they read. Most of the projects also have unit tests to check they’re working correctly. Checkout the GitHub repository.
Computer Science from Scratch just came out so it has hardly any reviews. But here’s some quotes about both it and some of my past books. The icons to the left of the reviewer name link to the source of the praise.
"I would highly recommend this book to any developer who enjoys learning through practical, project-based work and wants to build interesting and rewarding projects.”
"David is an excellent programmer and teacher and you should buy his book if you have ANY interest in algorithms period.”
"Computer Science degree condensed into a thin book"
“...this is a fantastic book, and I am confirming what I do know and learning all kinds of new information."
“Classic Computer Science Problems seems to understand the psychological quirks of software developers and how to use them as a motivational tool."
I’m David Kopec, an Associate Professor of Computer Science at Albright College in Reading, Pennsylvania, USA. Before my decade in academia, I worked as a software developer. Computer Science from Scratch is my fifth book. I think it’s my most fun. I’m also a podcaster, open source project maintainer, and indie app developer.