In Python you have the following arithmetic operators:
Now, let's try to use Python as a calculator. We will calculate :
Here are a few more examples:
The rules of arithmetic precedence are the same as with calculators; Python evaluates expressions from left to right, but things enclosed in brackets are evaluated first:
You can also do some calculations using built-in elementary functions. For this we will need to import a module (i.e. peace of code) which contains such functions. For elementary functions, we will need to import the module math which has elementary functions. Let calculate :
Note we call “math.sqrt”, not simply “sqrt”.
Alternatively, you simply import all functions (in this case you do not need to type “math.” on front of each statement
Let us consider a logarithmic function:Exercise: Change 100 to some other number and check the output
The math module provides access to mathematical constants and functions.
How do we know which functions can be used? There is a special command “dir()” which prints all implemented Python functions in the module “math”:
Run this commands and you will see familiar math functions of Python
Let us find out max and value for integer numbers: