Python calculator

In his two most recent posts, Clark Goble talks about using Python/iPython interactively and gives some helpful and fun tips.

In this one, he shows how to use the colorama library and Emoji fonts to spruce up your iPython prompt. It’s a clever follow-on to his earlier post on adding Emoji characters to your bash prompt.

In this post, which is primarily about configuring bc to work nicely as a Terminal calculator, Clark says he prefers bc to iPython most of the time because iPython takes too long to start up when all he wants to do are a few simple calculations. I agree about iPython—in fact, I’ve never really been comfortable in iPython—but I don’t like bc either. In the past, I’ve used Octave as my calculator, but recently I’ve been using the regular Python interpreter in interactive mode.

Without libraries, Python is a little bare-bones for a calculator, so I’ve set things up to provide a more pleasant environment. First, I have the following definition in my .bashrc file:

# Set Python startup environment
export PYTHONSTARTUP=$HOME/.pythonrc

The PYTHONSTARTUP environment variable tells Python which file to read in when starting up an interactive session. My .pythonrc file looks like this:

from __future__ import division
from math import *
from trigd import *

Not too complicated. The first line tells Python to treat integers as floating point numbers when using the usual division operator, /. I deal almost exclusively in real numbers, and it does me no good to type 3/4 and get 0 back. If I really need to do do integer division, I can use the // operator.1

The second line imports all the functions and constants in the standard math library, which I can’t live without, and the third line imports the functions of my own trigd library. This is a very simple library with trig functions that take arguments in degrees and inverse trig functions that return results in degrees. Here’s that library in its entirety:

 1:  from math import cos, sin, tan, acos, asin, atan, atan2, degrees, radians
 3:  def cosd(x):
 4:    return cos(radians(x))
 6:  def sind(x):
 7:    return sin(radians(x))
 9:  def tand(x):
10:    return tan(radians(x))
12:  def acosd(x):
13:    return degrees(acos(x))
15:  def asind(x):
16:    return degrees(asin(x))
18:  def atand(x):
19:    return degrees(atan(x))
21:  def atan2d(y, x):
22:    return degrees(atan2(y, x))

Fortran programmers will look at functions like cosd and smile. These functions, while not (I think) part of the standard, are convenience features in many Fortran compilers. Octave has them, too.

These customizations of the Python’s interactive environment don’t address one of Clark’s complaints about using Python as a calculator: that you often get results like

>>> sind(30)

instead of the nicely rounded answers you’d get from a real calculator. I’m sympathetic to that complaint, but I’m so used to that kind of answer that it really doesn’t bother me.

  1. Note: if you’re using Python 3, this behavior is already built in, and you don’t have to do an import. Of course, if you’re using Python 3, you probably already know that.