Python's inherent clear syntax enables it to function as a competent CLI calculator out of the box. With (very) small modifications, it becomes even better. A small guide follows.

Install Python

This is most probably already installed. Otherwise,

sudo aptitude install python

should suffice.

The calculator

Python's inherent syntax enables intuitive code such as


to work without modification. Combined with the interactive interpreter, this is almost all that is needed, right out of the box.

The interactive interpreter

Started the interactive interpreter by issuing:


End it by pressing Ctrl+d at an empty line.


Python 2.7 which is the standard version of Python in Debian (and probably will be for quite some time) by default works with integer division:

>>> (5+7)*14/3
>>> (5+7)*14/3/5

Because of Python giving the answer as an integer, rounding errors are introduced. However, if a number in the calculation is a floating number, the calculation is automatically transformed to float precision:

>>> (5+7)*14/3/5.0

If one knows this, it's not a big problem, but it is easy to forget and not intuitive when doing classic math. In Python 3, the (distantly) upcoming standard version of Python, float division is the default. This behaviour can be imported into Python 2.7, which makes for a better calculator:

>>> from __future__ import division
>>> (5+7)*14/3/5


The syntax is not as intuitive for exponentiating:

>>> 2^10

The ^ ("caret") symbol actually denotes a bitwise XOR operation in Python. The exponential is instead accessed through **:

>>> 2**10

Math functions

A simple arithmetic calculator might be useful, but we can do more. Python's math module gives access to many essential functions:

>>> from math import *
>>> 2*sin(radians(666)) + (1+sqrt(5))/2 + exp(pi) - abs(e**(1j*pi))*factorial(20)/factorial(19)

(Rats, almost a handy way to calculate π... :-) )

In regular coding, I would not import math as *, but instead address it by e.g. math.factorial(20), but in a dedicated calculator I indulge myself and import the complete module into the namespace.


>>> a=b=5
>>> c=b+1
>>> a+b+c
>>> _**2
>>> _**2

Complex numbers, matrices, etc.

There are Python modules to support more advanced complex calculations (cmath), linear algebra (numpy) and more, but I would recommend instead using e.g. Wolfram Alpha if one needs to do that complex (aha) calculations on the fly. Python should not be underestimated as an advanced numerical analysis tool in general, though; with numpy it in many respects equals e.g. MATLAB.

Script it

It's not much code, but to work as a handy calculator it must not be needed to import the math module and float division every time. Create the file calc in your path with the following contents:


#Possible addition:
#  from numpy.linalg import norm;\
#but it adds a second or two to initial startup time and is not that frequently needed.

ARGS=$(cat -- <<-ARGS
   # Use floating point division by default
   from __future__ import division
   # Import common math functions into default namespace
   from math import *
   # Define some common constants
   # Alias log() (the natural logarithm) to ln()

if [ ${#} -eq 0 ]; then
    python -ic "${ARGS}"
    python -c "${ARGS}; print ${*};"

(online repository available) and make it executable. calc is now a fully-fledged (*cough*) calculator! If called with arguments, it will return the computed sum. If called without arguments, a Python interpreter will be started.

Bind its execution to a keyboard shortcut (in my case: Mod4+curxvtc -e calc) and amaze your friends with your lightning fast math skills.


$ echo "(1+sqrt(5))/2" | calc
>>> 1.618033988749895