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

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

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:

```
python
```

End it by pressing `Ctrl+d`

at an empty line.

### Division

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
56
>>> (5+7)*14/3/5
11
```

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
11.2
```

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
11.2
```

### Exponentials

The syntax is not as intuitive for exponentiating:

```
>>> 2^10
8
```

The `^`

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

:

```
>>> 2**10
1024
```

### 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)
3.140692632779267
```

(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.

### Variables

```
>>> a=b=5
>>> c=b+1
>>> a+b+c
16
>>> _**2
256
>>> _**2
65536
```

### 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:

```
#!/bin/sh
#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
c=299792458
g=9.82
# Alias log() (the natural logarithm) to ln()
ln=log
ARGS
)
if [ ${#} -eq 0 ]; then
python -ic "${ARGS}"
else
python -c "${ARGS}; print ${*};"
fi
```

(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+c`

→ `urxvtc -e calc`

) and amaze your friends with your lightning fast math skills.

## Encore

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