6 months ago

Happy Pi Day: Code Pi with Raspberry Pi and Python

You want Pi; we’ve got Pi; and a Raspberry Pi with a side order of Python. This Pi Day, why not use a Raspberry Pi to learn all about one of the most important numbers to exist?

The Pi symbol 

Every March the 14th we celebrate Pi Day (3/14). It’s a special day for us because we love math(s), and programming, and the ‘Pi’ in Raspberry Pi is short for Python. So says Eben:

Pi is because originally we were going to produce a computer that could only really run Python. So the Pi in there is for Python. Now you can run Python on the Raspberry Pi, but the design we ended up going with is much more capable than the original.

So this year we thought we’d dig a little deeper into pi and learn a little more with our favourite computer.

Calculate pi in Python (on a Raspberry Pi)

It’s actually pretty easy to access pi on a Raspberry Pi. Open Python (choose Menu > Programming > Python 3 (IDLE) ) and enter the following:

from math import pi

pi

This will return pi to 15 decimal places:

3.141592653589793

Pi in Python

Using the math.pi constant in Python

Sure, that’s pretty easy. But how is pi calculated in the first place? And what if we wanted to calculate pi to more decimal places?

For that we need a more detailed program. Open File > New and save the file as ‘pi.py’. Now enter the following code (credit: Martin Thoma, Stack Overflow):

Press F5 to run the program and enter:

print(pi)

And you’ll get pi to 1000 decimal places. Much more pi than math.pi! Change the .prec value to get even more detail.

decimal.getcontext().prec = 2000

This code uses Issac Newton’s formula for calculating Pi. You can learn more about it at Pi314.

Calculate Pi in Python

Use a Raspberry Pi and Python to calculate pi this Pi Day!

Learn pi with Khan Academy

We can’t recommend Khan Academy highly enough.  Khan Academy is a non-profit educational organisation, and all of its videos and interactive lessons are free.

Salman Khan created the site in 2008, and his calm voice walks through mathematical concepts while he sketches on the screen using SmoothDraw 3 and a Wacom Bamboo tablet.

All of the videos are available on YouTube and, far from being a dry, disinteresting lecture, they quickly capture the joy of mathematics.

The circle is arguably the most fundamental shape in our universe, whether you look at the shapes of orbits of planets, whether you look at wheels, whether you look at things on kind of a molecular level.  They kept measuring it better and better and better, and then they realised that they were getting this number: 3.14159. And they just kept adding digits and it would never repeat. It was a strange fascinating metaphysical number. And it just showed up for every circle. The ratio of the circumference of the diameter was this kind of magical number. So they gave it a name; they called it ‘pi’.

Many people (including us) drop into Khan Academy on a regular basis to brush up on a math(s) subject. But Salman Khan also creates fabulous resources such as this Discovering Pi document (developed with NASA).

Discovering Pi

Discovering pi with Khan Academy

Happy Pi Day!