# Using Python Projects to Make a Better Math Class

## Equations are fun, but the real insights happen when kids explore math with code

*Peter Farrell spent more than a decade teaching math and computer science. Somewhere along the way, he began using Python to create programming challenges to pair with his lessons. But what started as a way to reinforce math concepts gradually developed into something else — a gateway to a more practical approach to math education.*

*Peter saw how coding projects allowed students to shift from passively learning concepts to actively working, reasoning, and playing with them. In other words, code helped them to go from learning about math to actually **doing** math. As he says “Why should the science, art, and home-ec students have all the fun? It’s about time we heard students saying **Look what I made in math class!**”*

Let’s start with a simple math lesson. A *centroid* is the exact middle of a shape — technically, it’s the average of every point in the shape. A centroid in math is similar to the idea of center of mass in a physical object.

Now take a look at the following picture. It shows two ways to think about — and learn about — centroids.

On the left, you see an example of a traditional approach to teaching math, involving definitions, propositions, and proofs. This method requires a lot of reading and odd symbols. You’d never guess this had anything to do with geometric figures. And you certainly won’t understand *why* you should be interested in finding the center of a triangle in the first place.

On the right, you see a picture of a dynamic sketch with a hundred or so rotating triangles. It’s a challenging programming project that I develop as an exercise in my book, Math Adventures in Python. If you want this picture to rotate the right way (and look cool), you have to find the centroid of each triangle. In other words, it’s a practical problem that you can *solve* with math. If you don’t have the math, you can’t get the picture.

So which approach would you prefer?

A student who knows math and can create cool designs is more likely to delve into a little geometry and put up with a few square roots or a trig function or two. A student who doesn’t see any outcome, and is only doing homework from a textbook, probably doesn’t have much motivation to learn geometry. Too often, this is the problem with traditional school math.

# The Problem with School Math

What do I mean by “school math” exactly? In the US in the 1860s, school math was preparation for a job as a clerk, adding columns of numbers by hand. Today, jobs are different, and the preparation for these jobs needs to change to match.

People learn best by doing. But this hasn’t been a daily practice in schools, which tend to favor passive learning. “Doing” in English and history classes might mean students write papers or give presentations. Science students perform experiments or build simple mechanisms. But what do math students do?

It used to be that all you could actively “do” in math class was solve equations, factor polynomials, and graph functions. But now that computers can do most of those calculations for us, these practices are no longer enough. Simply learning how to automate solving, factoring, and graphing is not the final goal. Once a student has learned to automate a process, they can go further and deeper into a topic than was ever possible before.

Let’s look at another example. Here’s a typical math problem you’d find in a textbook. It asks students to define a function, “f(x),” and evaluate it for a long list of values.

This same format goes on for 18 more questions! This kind of exercise is a trivial problem for a programming language like Python. To solve it there, we can simply define the function `f(x):`

import mathdef f(x):

return math.sqrt(x + 3) — x + 1

And then plug in the values by iterating over a list, like this:

`#list of values to plug in`

for x in [0,1,math.sqrt(2),math.sqrt(2)-1]:

print("f({:.3f}) = {:.3f}".format(x,f(x)))

The last line just makes the output pretty while rounding all the solutions to three decimal places. Here’s the result:

`f(0.000) = 2.732 `

f(1.000) = 2.000

f(1.414) = 1.687

f(0.414) = 2.434

In programming languages like Python, JavaScript, C#, and so on, functions are a vitally important tool for transforming numbers and other objects — and even other functions! In other words, this core abstraction in math becomes an essential *tool* in code. Once again, we’ve gone from thinking about the abstract world of equations to working in the hands-on world of projects.

Here’s another example. A math textbook published in the 21st century, decades after Benoit Mandelbrot first generated his famous fractal on a computer when working for IBM, shows a picture of the Mandelbrot set and gushes over the discovery. The textbook describes the Mandelbrot set, shown here, as “a fascinating mathematical object derived from the complex numbers. Its beautiful boundary illustrates chaotic behavior.”

The textbook then takes the reader through a painstaking “exploration” to show how to transform a point in the complex plane. But the student is only shown how to do this on a calculator, which means only two points can be transformed (iterated seven times) in a reasonable amount of time. *Two points!*

This is another case where Python comes to the rescue. Even a beginning coder can learn to make a program that transforms *hundreds of thousands* of points automatically and even draw the Mandelbrot set you see above!

In my eight years of experience as a math teacher and three years of experience as a computer science teacher, I’ve met many more math learners who prefer the visual approach to the academic one. In the process of creating something interesting, students come to understand that math is not just following steps to solve an equation. They see that exploring math with programming allows for many ways to solve interesting problems, with many unforeseen mistakes and opportunities for improvement along the way.

*Enjoyed this article? You can work through all the projects in Peter Farrell’s book, **Math Adventures with Python**. Peter Farrell also blogs on Twitter as **Techy Math**.*