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 143 >>> _ + 7 150 >>> _ / 2.0 75.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
You can quit with CTRL+D.
I use this calculator quite often, so I added the following line to my