How to use Python as a command line calculator

0

Need to do some quick math? Python supports you.


Calculator with plans

Although you can use a graphing calculator on your computer, the Python programming language interpreter can also serve as a desktop calculator. It’s such a popular joke in the Python community that it’s mentioned in the official tutorial. Here is how you can use Python as a calculator.

Start Python

How you start the Python interpreter depends on the system you have. On Linux, macOS, or Windows with the Windows Subsystem for Linux, all you need to do is type “python” or “python3” into the terminal command prompt.

Related: How to run a Python script

Arithmetic operations

Python interpreter doing an addition

When you launch the Python interpreter, you end up at the Python prompt. Arithmetic operators are familiar if you’ve used a calculator before.

The addition is simple:

2 + 2

The interpreter will of course return “4”.

The subtraction is the same.

4 - 2
2

You can also multiply. This uses the * symbol.

42 * 23
966

The division uses the / operator. In Python 3, this will return the rest as a decimal fraction:

15 / 4
3.75

Exhibitors use the ** operator:

7**2
49

More advanced mathematics

Sometimes you want to do more advanced calculations than what Python’s built-in arithmetic operations offer. As with other programming languages, Python uses libraries to extend its functionality, and math is no exception. As part of its standard library, Python includes the Math library.

To use it, just type this in the interpreter prompt:

import math

The math library comes with trigonometric functions as well as information about numbers. It also includes an approximation of pi. Let’s use this to prove that you should always order a bigger pizza, as you will get more for your money as the area increases with the square of the radius with a round pizza. Remember, the formula for the area of ​​a circle is pi times the radius (half the diameter) of the squared circle.

For example, here is the area of ​​an 8 inch pizza with Python:

math.pi * 4**2
50.26548245743669

The rounded answer is 50.27 square inches. And for a 16 inch pizza:

math.pi * 8**2 
201.06192982974676

A 16 inch pizza has an area of ​​201.06 square inches. That’s almost four times more pizza than the 8 inch pizza!

A handy calculator for the command line

Having the calculator run in a background window makes it easy to perform simple calculations. Using Python is faster than fumbling around with a graphing calculator. If you don’t want to use the terminal or your device doesn’t, you can use a web-based Python interpreter instead.


python online
Try Python in your browser with these free online interactive cases

Whether you’re browsing these Python examples or going over the basics of arrays and lists, you can test the code right in your browser. Here are the best online Python interpreters we’ve found.

Read more


About the Author


Source link

Share.

Leave A Reply