There’s a calculator hiding in your Mac

Mac OS X has a bunch of cool features, but there’s one that I haven’t read much about on the Web: a built-in fully programmable calculator. It doesn’t look like a calculator a first glance, but if you’re willing to learn a bit you’ll be rewarded with a very powerful computing tool.

Every Mac running a recent version of OS X includes a full version of the Python programming language. Python, which is named after the British comedy troupe Monty Python and not the large snake, was invented about ten years ago and has grown into one of the most popular open-source scripting languages around. I taught Python as an introductory programming language for a couple years and I loved how powerful and easy to learn it was for beginning computer programmers. But you don’t have to be a computer programmer to use it as a programmable calculator.

To start Python, open the Termal application that’s found in the Utilities folder inside your Applications folder. After double-clicking on the Terminal icon, start python by typing “python” at the prompt.

Tim-Wilsons-Computer:~ wilson$ python
Python 2.3 (#1, Sep 13 2003, 00:49:11)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> is the Python prompt. Simple calculations work just as they do on a regular calculator and will be familiar to anyone who uses a TI calculator. One difference is that you use ** to raise a number to a power.

>>> 4*12
48
>>> 34+89
123
>>> 42.1/83.2
0.50600961538461542
>>> (2+3)*(6-2)
20
>>> 2**3
8

More advanced math functions can be used, but you have to import them first by typing “import math” at the prompt.

>>> import math
>>> math.sqrt(12)
3.4641016151377544
>>> math.pi
3.1415926535897931
>>> math.exp(2)
7.3890560989306504

Here’s a simple example that demonstrates how you can use variables (remember that Python is a full-blown programming language) to store values as you go. The extra spaces around the equals and plus signs are included for readability. You can leave them out in practice.

>>> bill = 19.42
>>> tax = bill * .065
>>> tip = bill * .15
>>> bill + tax + tip
23.595300000000002

These brief examples don’t begin to show the power of Python. Scientists around the world use Python every day for enormously complex tasks. If you’d like to learn more about Python, check out some of the links at the Python In Education Special Interest Group.

One thought on “There’s a calculator hiding in your Mac