Puzzle 2 A Slice of π

The Pragmatic Programmers
The Pragmatic Programmers
2 min readMar 23, 2022

--

Python Brain Teasers — by Miki Tebeka (11 / 40)

👈 Ready Player One | TOC | When in Kraków 👉

pi.py

​ ​π​ = 355 / 113
​ ​print​(​π​)

Guess the Output

IMPORTANT

Try to guess what the output is before moving to the next page.

images/hline.png

This code will print: 3.1415929203539825

images/hline.png

There are two surprising things here: one is that π is a valid identifier, and the second is that 355 / 113 computes to a float.

Let’s start with π (the Greek letter pi). Python 3 changed the default encoding for source files to UTF-8 and allows Unicode identifiers.

This can be fun to write, but in practice it’ll make your coworkers’ lives harder. I can easily type π in the Vim editor that I use; however, most editors and IDEs will require more effort.

One area where I’ve found that Unicode identifiers are helpful is when translating mathematical formulas to code. Apart from that, stick to plain old ASCII.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.