Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Python as a Calculator

We can use python as a calculator to perform basic arithmetic operations. Python can handle addition, subtraction, multiplication, and division just like a standard calculator. Jupyter Notebook or python shell can be used to execute these operations interactively.

To trigger python shell, make sure you have python installed on your system. You can check this by running python --version in your terminal or command prompt. If you have python installed, you can start the python shell by typing python or python3 depending on your installation.

And finally to start your shell, type python or python3 in your terminal or command prompt.

Basic Arithmetic Operations

TRY IT! Compute the sum of 1 and 2.

In [1]: 1 + 2
Out[1]: 3

An order of operations is a standard order of precedence that different operations have in relationship to one another. Python utilizes the same order of operations that you learned in grade school. Powers are executed before multiplication and division, which are executed before addition and subtraction. Parentheses, (), can also be used in Python to supersede the standard order of operations.

TRY IT! Compute 34(22+4/2)\frac{3*4}{(2^2+4/2)}.

In [2]: (3*4)/(2**2 + 4/2)
Out[2]: 2.0

TIP! You may have noticed Out[2] is the resulting value of the last operation executed. You can use _ symbol to represent this result to break up complicated expressions into simpler commands.

TRY IT! Compute 3 divided by 4, then multiply the result by 2, and then raise the result to the 3rd power.

In [3]: 3/4
Out[3]: 0.75

In [4]: _*2
Out[4]: 1.5

In [5]: _**3
Out[5]: 3.375

Python has many basic arithmetic functions like sin, cos, tan, asin, acos, atan, exp, log, log10 and sqrt stored in a module math.

In [6]: import math

TRY IT! Find the square root of 4.

In [7]: math.sqrt(4)
Out[7]: 2.0

TRY IT! Compute the sin(π2)sin(\frac{\pi}{2}).

In [8]: math.sin(math.pi/2)
Out[8]: 1.0

TRY IT! Compute elog10e^{log10}.

In [9]: math.exp(math.log(10))
Out[9]: 10.000000000000002

Note that the log function in Python is logelog_e, or the natural logarithm. It is not log10log_{10}. If you want to use log10log_{10}, you need to use math.log10.

TRY IT! Compute e34e^\frac{3}{4}

In [10]: math.exp(3/4)
Out[10]: 2.117000016612675

TRY IT! Use the question mark to find the definition of the factorial function

In [11]: math.factorial?
Signature: math.factorial(x, /)
Docstring:
Find x!.

Raise a ValueError if x is negative or non-integral.
Type:      builtin_function_or_method

Python will raise an ZeroDivisionError when you have expression 1/0, which is infinity, to remind you.

In [12]: 1/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-12-9e1622b385b6> in <module>()
----> 1 1/0

ZeroDivisionError: division by zero

You can type math.inf at the command prompt to denote infinity or math.nan to denote something that is not a number that you wish to be handled as a number. If this is confusing, this distinction can be skipped for now; it will be explained more clearly when it becomes important. Finally, Python can also handle the imaginary number.

TRY IT! 1/1/\infty, and 2\infty*2 to verify that Python handles infinity as you would expect.

In [13]: 1/math.inf
Out[13]: 0.0

In [14]: math.inf * 2
Out[14]: inf

TRY IT! Compute /\infty/\infty

In [15]: math.inf/math.inf
Out[15]: nan

TRY IT! Compute sum 2 + 5i

In [16]: 2 + 5j
Out[16]: (2+5j)

Note that, in Python imaginary part is using j instead of i to represent.

Another way to represent complex number in Python is to use the complex function.

In [17]: complex(2,5)
Out[17]: (2+5j)

Python can also handle scientific notation using the letter e between two numbers. For example, 1e6=10000001e6=1000000 and 1e3=0.0011e-3=0.001.

TRY IT! Compute the number of seconds in 3 years using scientific notation.

In [18]: 3e0*3.65e2*2.4e1*3.6e3
Out[18]: 94608000.0

TIP! Every time when we type the function in math module, we always type math.function_name. Alternatively, there is a simpler way, for example, if we want to use sin and log from math module, we could import them this way: from math import sin, log. Then you all you need to do when using these functions is using them directly, for example, sin(20) or log(10).

Basic Data Types

We just learned to use Python as a calculator to deal with different data values. In Python, there are a few data types we need to know, for numerical values, int, float, and complex are the types associated with the values.

  • int: Integers, such as 1, 2, 3, ...

  • float: Floating-point numbers, such as 3.2, 6.4, ...

  • complex: Complex numbers, such as 2 + 5j, 3 + 2j, ...

You can use function type to check the data type for different values.

TRY IT! Find out the data type for 1234.

In [19]: type(1234)
Out[19]: int

TRY IT! Find out the data type for 3.14.

In [20]: type(3.14)
Out[20]: float

TRY IT! Find out the data type for 2 + 5j.

In [21]: type(2 + 5j)
Out[21]: complex

Of course, there are other different data types, such as boolean, string and so on, we will introduce them later in the book.

Courtesy

This page is the summerized form of,

To learn more about Python as a calculator, you can refer to the original source.