Math Functions

Math Module

A Python module that provides access to mathematical functions. In other words, it is a library that we can bring in, to use functions defined within it. If you do not know what a function is, simply put it is a block of code that can be invoked at any time without having to re-write the identical block of code over again.

This module offers access to various mathematical functions which can be used to achieve various mathematical computations such as exponential, rounding, logarithmic, trigonometric, and so on. In this blog, I will demonstrate how to work with functions within this module.

FUNCTION 1: math.sqrt(x)

import math

variable_1 = 9

sq_root_value = math.sqrt(variable_1)

print("Square root is: ", sq_root_value)

Let’s break it down. First, we have the import statements. When working with modules in Python we need to import the modules we would like to use. There are pre-existing modules that come with Python and math is one of them. In the case it is not, we will have to download them. Secondly, we created a variable called “variable_1” that holds the value 9.

Thirdly we created a variable called “sq_root_value” which, as you might have guessed, will contain the square root of the value we pass (give) to the ‘sqrt’ function. Since the ‘sqrt’ function is from the math module, we have to first mention the module from which we want a specific function and then that function. A function takes in arguments (values) and can have more than one. In this particular case, we are passing the variable we created in line 2. Therefore, this line says we are asking Python to compute the square root of the value stored in “variable_1” (which is 9) and then store that computation of the square root in the variable “sq_root_value”.

Finally, we then print out that value onto the terminal. After we execute this code, we should get the following.

FUNCTION 2: math.pow(x, y)

import math

variable_1 = 9

power_variable = math.pow(9, 2)

print("9^2 gives: ", power_variable)

The ‘pow’ function gives us the value of xy. Notice that this function has 2 arguments, the first one ‘x’ is the base value, and the second one ‘y’ is the exponent. Here we have asked Python to give us the value of 92. When we execute this, we should get the following output.

Aside from these, another relevant function is:

  • round(number, digits)
    • It is part of the built-in functions in Python.
    • It rounds a number to the required number of digits.
    • It returns a type float.

 

Supposing we wish to see all of the available functions in the Math module then we simply have to code the following:

import math

all_functions = dir(math)

print(all_functions)

These are all of the functions within the math module, and below there is a brief explanation of some of the functions.

Serial No. Function Description
1. log(x[, base]) With only x, it returns the log of x with base e.

With x and base, it returns the log of x with the value in base.

2. log1p(x) Returns the natural log of 1+x, with base e
3. log2(x) Returns the base 2 log of x
4. log10(x) Returns the base 10 log of x.

(Gives a more precise value than log(x,10))

5. exp(x) Returns e raised to the power x, and e=2.7182…
6. expm1(x) Returns e raised to the power minus 1
7. sin(x) Returns the sine of x
8. cos(x) Returns the cosine of x
9. tan(x) Returns the tangent of x
10. dist(p,q) Returns the Euclidean distance between p & q, however, p & q must have the same dimensions

 

What have we learned?

  • What are modules?
  • What are functions?
  • What are arguments?
  • How do we import modules?
  • How do we call functions from a module?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments