Python Calculator

10 April 2020

I wanted to have a powerful calculator at my hand which I could open with a simple shortcut. Naturally, Python came to my mind, as it is preinstalled on any modern Linux and can calculate things with remarkable accuracy.

It's easy enough to bind a command to a hotkey and have it open a terminal with Python. (In Gnome3 you can go to settings → keyboard shortcuts → press + at the bottom.)

gnome-terminal -- python3

But this is not that ergonomic, since whenever you want to compute the sine, for example, you would need to run from math import sin. Doing this every time you need to use a mathematical functions in a calculator seems a bit dull. Luckily, you can run a custom command when opening python, in which you can import everything from the math module. The command line option -c allows you to specify a custom command to run. Since this would normally terminate the interpreter after running that command, you also need to pass -i to keep the interpreter in interactive mode.

gnome-terminal -- python3 -ic "from math import *"

I have bound this command to the calculator key on my keyboard, which is pretty nice.

Pro-Tip: You can use the variable _ to work with the previous result.