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
The desktop computer you've been waiting for: Raspberry Pi 500+
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

Raspberry Pi 500+ in Raspberry Pi Official Magazine issue 158
We’re quite taken with Raspberry Pi 500+. But when you don’t need all that processing power, and just want a board that will make a plastic skeleton jump around in a terrifying manner, you’ll find Raspberry Pi Pico more than up to the job. There’s more terror in the magazine (which is only right as […]
Read more →

Win one of five Raspberry Pi SSD 1TB
Raspberry Pi prides itself on high quality hardware, and this 1TB Raspberry Pi SSD is no different. You can use it with a standard Raspberry Pi or even in your desktop PC – the choice is yours. We have five to give away and you can enter below Win 1 of 5 Raspberry Pi SSD […]
Read more →

Dual-screen cyberdeck
Twin screens mean you can code on one screen while watching David Bowie’s 1978 Musikladen show on the other.
Read more →
Sign up to the newsletter
Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.