Why You Should Use Python as a Calculator (and How to Get Started)


Summary

  • Calculator apps and handheld calculators are clunky, but Python’s interactive mode easily recalls previous results for calculations. You can also scroll through and search input.
  • Python offers basic arithmetic functions similar to handheld calculators.
  • Python can tap into advanced libraries like math, NumPy, SciPy, and SymPy for more complex math operations beyond basic arithmetic.


A common joke among Python programmers is that the interactive mode works great as a desk calculator. Here are the reasons this is close to reality.



Calculators Have a Design Flaw

If you use a handheld calculator, you may have noticed something frustrating if you have a basic model: It’s hard to recall previous results when using them in new calculations.

You can save numbers to memory for later recall on some calculators, but you have to know how it works. And that means reading a manual. You did save the manual, didn’t you? Of course, you can usually find the manual online. That may be good enough for TI and Casio users, but many of the hardware calculators, or at least the ones you can buy today, come from generic makers. Good luck finding the instructions for them!

Calculator apps on your phone or computer aren’t much better. The main problem with these programs is that they try to make themselves look and work like handheld calculators. Among user interface designers, this is known as “skeuomorphism.” Software calculators feel awkward to use because you have to click buttons manually.


If you’re taking math or science courses, you’ll most likely only be allowed to use a handheld calculator on exams. When you’re on your own, you’re better off using something like Python.

Python’s Interactive Mode Is Easy to Use

While Python is meant for programming, it can solve a lot of the problems with handheld and software calculators.

Once you’ve installed Python and called up the interpreter from your system’s menu or typing python at the command line, you can use all of the standard arithmetic functions in Python just by typing them and pressing enter to execute at the prompt:

2 + 2
5 * 3
45 - 30
720 / 5
Simple arithmetic performed in the Python interpreter.

The operators work similarly to other calculators you might have used, such as + and – for addition and subtraction, and * and / for multiplication and division.


The standard Python interpreter uses the GNU Readline library and makes it easy to recall what you’ve previously typed. The up and down arrow keys allow you to move up and down through your input. You can also search your input by hitting Ctrl+r (lowercase r). Then start typing to find what you previously typed. You can move up and down through your input by using the arrow keys

The _ (underscore) variable stores the previous result. You can use this for new calculations. For example, to multiply the previous number by another number


_ * 42.

Or to square it:


_**2
Using the "_" operator to access previous results in the interactive Python interpreter.

You can also install iPython for even more powerful interactive operations. When you’ve finished using Python, to exit your session, type exit() or press Ctrl+D on Linux or macOS or Ctrl+Z in Windows (unless you’re running Python in Windows Subsystem for Linux, where you use Ctrl+D as in other Unix-like systems). But you’ll probably want to just leave it open. You never know when you might want to make a calculation, plus it’s a great excuse to learn Python.


Tap Into Powerful Libraries for More Advanced Math

While Python is great for simple math, it is capable of a lot more with the right libraries.

The built-in math library will turn Python into a scientific calculator. You can use the import function to access its functions.


import math

Suppose you wanted an approximation of pi. You can get it in the math library:


math.pi

The math library also has trigonometric functions. Suppose you wanted to find the sine of 45 degrees. The trig functions expect angles in radians, but the math library also has functions to convert between the two. To convert the angle to radians:


math.radians(45)

And to compute the sine of this result:

math.sin(_)

Trigonometric operations in Python using the math library.

You can chain the function arguments for fewer lines:



math.sin(radians(45))

There are many other libraries you can install if you have even more advanced math to do. These libraries, such as NumPy, SciPy, and SymPy, let you tackle anything from statistics to calculus to linear algebra to differential equations. The advantage of these libraries is that they’re free and open source. It’s why Python is becoming a language of choice for scientific computing and one more reason to learn the language.

A good example is SymPy, a computer algebra system. A computer algebra system works with symbolic math the way a calculator works with numeric math.

A key difference between the two is how it handles things like square roots. When you take the square root of 2 using the math library’s sqrt function, you get a floating point approximation because it’s not a perfect square and it’s an irrational number.

When you take a square root in SymPy, a square root that’s not a square root is printed as it would look in a textbook. It will also automatically factor out any perfect squares.


Below is a special interactive interpreter supplied with SymPy that’s geared for interactive sessions. I’m showing the square root operation from the math library and SymPy. Notice that the latter even has a square root symbol?

Python math library square root and SymPy square root in iPython/

SymPy is even more powerful, letting you solve equations and take derivatives and integrals of functions. All of these libraries are powerful tools to get into. SageMath is a full-blown environment based on Python that aims to compete with Mathematica, Maple, and MATLAB.

If you’re studying these subjects, Python will let you focus on the problem instead of the calculations.



Source link

Previous articleThe Kobo Libra Color, Tested and Reviewed 2024
Next articleBitcoin Turns 16: Celebrating Genesis Day and $BTC’s Legacy