In Python there is the standard math library that makes it easy to perform numeric operations. For example, the logarithm, square root, and trigonometric functions are built-in and can be used directly in the standard library.

Python has a built-in standard module, math, which contains many methods for performing ordinary calculations. This module greatly facilitates our work when we are writing operations to perform some mathematical calculations. We will show some examples below how we use it in some operations. For a complete list of all operations available, see math – mathematical functions (Python docs).

In chapter 5 we will see more about how functions in Python and how we can create our own functions, but for now, we are want to see how we can use the built-in function Math to more easily perform different mathematical calculations. Keep in mind that it is important to keep track of what data type (input) that is compatible with the calculation you intend to use

This module greatly facilitates out work when we are performing mathematical calculations

Below are some examples of functions found in the math module and examples of how to use them.

Before we start using the *Math *module, we need to import the library by writing.

import math

Note, this only needs to be done once.

For example, say you would like to calculate 2 ^ 8 (two raised by eight). It may be difficult (and tiring) to count on paper, but in Python you make it easy with the pow (x, y) function.

power = math.pow(2,8) print(power)

Giving the result:

256.0

The square root is easily calculated with the sqrt (x) function

sqrtroot = math.sqrt(12) print(sqrtroot)

Giving the result:

3.4641016151377544

The exponential function e ^ x is easily calculated with the exp (x) function

exp = math.exp(2) print(exp)

Giving the result

7.38905609893065