Home > bash, python > Command line calculator with Python

Command line calculator with Python


You use the command line terminal a lot and sometimes you need a simple calculator. Is one available in command line?


Of course. It’s called Python interpreter :) Python is usually available everywhere and its command line interpreter is a perfect calculator too.


C:~> python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 45 + 98
>>> _ + 7
>>> _ / 2.0

Cannot be any simpler. As you can see, the special variable ‘_‘ (underscore) gets the value of the previous computation. Division between two integers returns the whole part, thus 7 / 2 is 3. What you need is usually 7 / 2.0, which is 3.5.

You can quit with CTRL+D.


I use this calculator quite often, so I added the following line to my ~/.bashrc:

alias p='python'
Categories: bash, python Tags: ,
  1. SzG
    July 15, 2011 at 02:38

    I use ogc. It’s faster than anything else I’ve seen. For instance a missing operand just means the last result. Like “+2” adds two to it, “*” squares it and “@r” takes its square root.
    It can do binary, octal, decimal and hex conversions as well, like “xFFFFd” prints 65535.
    If you need just one small calculation, it can take it as parameters, without going interactive: “ogc -e 255b” prints it in binary format.
    It also handles integers / floats in a Pythonic manner: 3/2 is 1, but 3/2.0 is 1.5.
    Unlimited undo with “u”. 20 memory registers.
    See http://linux.subogero.com/ogc-cli-calculator/

  2. SzG
    July 15, 2011 at 02:44

    Ja, hogy te is magyar vagy… :-)

    • July 15, 2011 at 08:48

      Igen :) Koszi az elozo hozzaszolast, ki fogom probalni. Udv.: Laci

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: