Happy Pi Day: Code Pi with Raspberry Pi and Python
By Lucy Hattersley. Posted
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?
Advertisement
Get started with Raspberry Pi – everything you need to know to start your journey!
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
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):
import decimal
def pi():
"""
Compute Pi to the current precision.
Examples
--------
>>> print(pi)
3.141592653589793238462643383
Notes
-----
Taken from https://docs.python.org/3/library/decimal.html#recipes
"""
decimal.getcontext().prec += 2 # extra digits for intermediate steps
three = decimal.Decimal(3) # substitute "three=3.0" for regular floats
lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
while s != lasts:
lasts = s
n, na = n + na, na + 8
d, da = d + da, da + 32
t = (t * n) / d
s += t
decimal.getcontext().prec -= 2
return +s # unary plus applies the new precision
decimal.getcontext().prec = 1000
pi = pi()
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.
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).
Happy Pi Day!
Lucy is Editor of Raspberry Pi Official Magazine.
Subscribe to Raspberry Pi Official Magazine
Save up to 37% off the cover price and get a FREE Raspberry Pi Pico 2 W with a subscription to Raspberry Pi Official Magazine.
More articles
Get started with Raspberry Pi in Raspberry Pi Official Magazine 161
There’s loads going on in this issue: first of all, how about using a capacitive touch board and Raspberry Pi 5 to turn a quilt into an input device? Nicola King shows you how. If you’re more into sawing and drilling than needlework, Jo Hinchliffe has built an underwater rover out of plastic piping and […]
Read more →
Win one of three DreamHAT+ radars!
That’s right, an actual working radar for your Raspberry Pi. We reviewed it a few months ago and have since been amazed at some of the projects that have used it, like last month’s motion sensor from the movie Aliens. Sound good? Well we have a few to give away, and you can enter below. […]
Read more →
RP2350 Pico W5 review
It’s Raspberry Pi Pico 2, but with a lot more memory
Read more →


