Math Adventures with Python

Math Adventures with Python

An Illustrated Guide to Exploring Math with Code
by Peter Farrell
January 2019, 304 pp.
Full color

Download Chapter 5: Transforming Shapes with Geometry
Download the code.
Download solutions to exercises.

Math Adventures with Python will show you how to harness the power of programming to keep math relevant and fun. With the aid of the Python programming language, you’ll learn how to visualize solutions to a range of math problems as you use code to explore key mathematical concepts like algebra, trigonometry, matrices, and cellular automata.

Once you’ve learned the programming basics like loops and variables, you’ll write your own programs to solve equations quickly, make cool things like an interactive rainbow grid, and automate tedious tasks like factoring numbers and finding square roots. You’ll learn how to write functions to draw and manipulate shapes, create oscillating sine waves, and solve equations graphically.

You’ll also learn how to:

  • Draw and transform 2D and 3D graphics with matrices
  • Make colorful designs like the Mandelbrot and Julia sets with complex numbers
  • Use recursion to create fractals like the Koch snowflake and the Sierpinski triangle
  • Generate virtual sheep that graze on grass and multiply autonomously
  • Crack secret codes using genetic algorithms

As you work through the book’s numerous examples and increasingly challenging exercises, you’ll code your own solutions, create beautiful visualizations, and see just how much more fun math can be!

Author Bio 

Peter Farrell is a math and computer science teacher with a passion for customizing ("hacking") math education and learning with technology. He lives in the San Francisco Bay Area with his wife and children.

Table of contents 


Part 1: Hitchin' Up Your Python Wagon
Chapter 1: Drawing Polygons with Turtles
Chapter 2: Making Tedious Arithmetic Fun with Lists and Loops
Chapter 3: Guessing and Checking with Conditionals

Part 2: Riding into Math Territory
Chapter 4: Transforming and Storing Numbers with Algebra
Chapter 5: Transforming Shapes with Geometry
Chapter 6: Creating Oscillations with Trigonometry
Chapter 7: Complex Numbers
Chapter 8: Using Matrices for Computer Graphics and Systems of Equations

Part 3: Blazing Your Own Trail
Chapter 9: Building Objects with Classes
Chapter 10: Creating Fractals Using Recursion
Chapter 11: Cellular Automata
Chapter 12: Solving Problems Using Genetic Algorithms


View the detailed Table of Contents
View the Index


Featured in interviews on, Mouse Vs. Python, and

"We have entered an era where learning Python is as essential as knowing the analytical skills of mathematics, and so from students to teachers, Math Adventures with Python is an indispensable book for all."
—Gabrielle Birkman, Mathematician and Writer

"Peter Farrell subverts the structure of a traditional math textbook and adopts an approach centered on student creation and discovery. Rather than treating mathematical facts as symbolic formalism to be mindlessly imitated by students, Math Adventures with Python seeks to teach mathematics through guided discovery. "
Dr. Tien Chih, Professor of Mathematics, Montana State University-Billings

"If you’re a mathy type and want to get into some computer programming, or if you’re a computer person but want to learn what kinds of math you can do with code, Math Adventures with Python will help you achieve those goals."
—Jenny Bristol, GeekDad


View the latest errata.