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.
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 .
In [2]: (3*4)/(2**2 + 4/2)
Out[2]: 2.0TIP! 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.375Python has many basic arithmetic functions like
sin,cos,tan,asin,acos,atan,exp,log,log10andsqrtstored in a modulemath.
In [6]: import mathTRY IT! Find the square root of 4.
In [7]: math.sqrt(4)
Out[7]: 2.0TRY IT! Compute the .
In [8]: math.sin(math.pi/2)
Out[8]: 1.0TRY IT! Compute .
In [9]: math.exp(math.log(10))
Out[9]: 10.000000000000002Note that the
logfunction in Python is , or the natural logarithm. It is not . If you want to use , you need to usemath.log10.
TRY IT! Compute
In [10]: math.exp(3/4)
Out[10]: 2.117000016612675TRY 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_methodPython 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 zeroYou can type
math.infat the command prompt to denote infinity ormath.nanto 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! , and 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]: infTRY IT! Compute
In [15]: math.inf/math.inf
Out[15]: nanTRY 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, and .
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.0TIP! 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]: intTRY IT! Find out the data type for 3.14.
In [20]: type(3.14)
Out[20]: floatTRY IT! Find out the data type for 2 + 5j.
In [21]: type(2 + 5j)
Out[21]: complexOf 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.