We know a module is a file that contains a collection of related functions and other definition or statements. There are several modules present in the Python standard library and are automatically installed with your python installation.

These modules are built-in modules in Python. In this tutorial, we will explore the capabilities of math module present in Python standard library and how will we use in the Python programming.

A **math module in Python** is a built-in module that contains a set of commonly used mathematical functions and constants for performing various mathematical tasks.

To use functions from the math module, we need to import it into the Python program code or script using the import statement. The general syntax to import math module in the program is as:

import math

After importing this module, we can call any functions of the math module. If we write the code to print the math module object, we will get some information about it.

import math print(math)

Output: <module 'math' (built-in)>

The module object contains various functions and constants defined in the module. To access any function or constant, we have to specify the name of module and the name of function separated by a dot operator (.) like this:

math.funct_name Or, math.var_name

Let’s take an example in which we will print the value of two mathematical constants pi and e.

# Importing the entire math module. import math # This statement will print the value of pi, which is an individual object of math module. print(math.pi) # This statement will print the value of e, which is an individual object of math module. print(math.e)

Output: 3.141592653589793 2.718281828459045

## Different Implements of Import Statement

There are the following ways of implementation of math module using import statement.

**Case 1:** **import math**

This statement imports the entire math module methods in the current application program. To use a math module, we must have to use the dot (.) notation with module name and function name.

**Example 1:**

# Importing the entire math module. import math # These statements will print the value of square root of 100 and 25. print(math.sqrt(100)) print(math.sqrt(25))

Output: 10.0 5.0

**Case 2: from math import sqrt**

The above statement imports only sqrt() method of the math module in the current application program. To use the sqrt() method we can avoid the dot notation with module name and function name.

**Example 2:**

# Importing only sqrt() function of math module. from math import sqrt # This statement will print the value of square root of 625. print(sqrt(625))

Output: 25.0

**Case 3: from math import sqrt, pi**

The above statement imports only two functions, sqrt and the value of a constant pi in the current application program.

**Example 3:**

# Importing only two functions sqrt() and value of pi. from math import sqrt, pi # This statement will print the value of square root of 25. print(sqrt(25)) # This statement will print the value of pi. print(pi)

Output: 25.0 3.141592653589793

**Case 4:** **from math import***

This statement imports all methods of math module in the current application program.

**Example 4:**

# Importing all functions math module. import math from math import* print(sqrt(25)) print(pi) print(floor(25)) print(pow(2, 3))

Output: 5.0 3.141592653589793 25 8.0

## List of Functions in Python Math Module

In Python, we can display the list of all functions available in the math module using the dir() function. This function returns a sorted list of names in the specified module, which includes function names, variables, and other attributes. Let’s write the code to print functions of math module in Python.

import math # Getting the list of function names in the math module. math_functions = dir(math) # Filter out only the function names from the list math_function_names = [name for name in math_functions if callable(getattr(math, name))] # Print the list of function names print(math_function_names)

Output: ['__loader__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nextafter', 'perm', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc', 'ulp']

## Power and Logarithmic Functions

**1. exp(x):** This function returns e raised to the power x (i.e. exponential of x: e^{x}), where e = 2.718281… is the base of natural logarithms. The general syntax to use exp() function is as:

math.exp(x)

In this syntax, x is a numeric expression. Let’s take an example based on it.

**Example 5:**

import math print(math.exp(4)) print(math.exp(math.pi))

Output: 54.598150033144236 23.140692632779267

**2. expm1(x):** This function returns e raised to the power x, minus 1. In simple, it returns the value of e^{x} – 1. Here, e is the base of natural logarithms. The general syntax is as:

math.expm1(x)

**Example 6:**

import math x = 1.5 expm1_value = math.expm1(x) print(expm1_value)

Output: 3.4816890703380645

In this example, we have calculated the value of e^{x} – 1 where x = 1.5 using the math.expm1() function provided by the math module.

**3. log(x[, base]):** This function returns the logarithm of a specified number x to a specified base. The square brackets around the base denote that the base parameter is optional. The general syntax is as:

math.log(x[, base])

**Example 7:**

import math x = 10 base = 2 # Calculate the logarithm of 'x' to the base 'base' result = math.log(x, base) print(result)

Output: 3.3219280948873626

In this example, we have calculated the logarithm of x = 10 to the base “base = 2”. The result is approximately 3.3219280948873626. If we omit the base parameter, the math.log() function will return the natural logarithm of x (base e). For example:

import math x = 10 # Calculate the natural logarithm of 'x' result = math.log(x) print(result)

Output: 2.302585092994046

**4. log1p(x):** This function returns the natural logarithm of 1 + x (base e), where “x” is a numeric value. It computes the logarithm of a number that is obtained by adding 1 to the input value “x.” We generally use to avoid loss of precision when calculating the logarithm of small values close to zero. The general syntax is as:

math.log1p(x)

**Example 8:**

import math # Example input value x = 0.5 # Calculate the natural logarithm of (1 + x). result = math.log1p(x) # Display the result print("The natural logarithm of (1 +", x, ") is:", result)

Output: The natural logarithm of (1 + 0.5 ) is: 0.4054651081081644

In this example, we have defined an example input value “x” as 0.5. Then, we have used the math.log1p(x) function to calculate the natural logarithm of (1 + x), and stored the outcome in the variable result. Finally, we have printed the result to see the output.

**5. log2(x):** This function provided by the math module returns the base-2 logarithm of the numeric value “x”. It simply calculates the natural logarithm of “x” with 2 as the base. The general syntax is as:

math.log2(x) # for x > 0

**Example 9:**

import math x = 8 # Calculate the base-2 logarithm of x. result = math.log2(x) # Display the result. print("The base-2 logarithm of", x, "is:", result)

Output: The base-2 logarithm of 8 is: 3.0

In this example, we have defined x = 8 and calculated the base-2 logarithm of “x” using math.log10(x). Then, we have stored the result in the variable result. Finally, we printed the result to see the output.

**6. log10(x):** This function returns the base-10 logarithm of the numeric value “x”. It calculates the logarithm of “x” with 10 as the base. The general syntax is as:

math.log10(x) # for x > 0.

**Example 10:**

import math x = 1000 # Calculate the base-10 logarithm of x result = math.log10(x) # Display the result print("The base-10 logarithm of", x, "is:", result)

Output: The base-10 logarithm of 1000 is: 3.0

In this example, we have defined x = 1000. Then, we have calculated the base-10 logarithm of “x” and stored the result in the variable result. Finally, we printed the result to see the output, which, in this case, would be 3.0 since 10^3 is equal to 1000.

**7. pow(x, y):** This function of math module returns x raised to the power y (x^y). It simply calculates “x” raised to the exponent “y”. This operation is equivalent to the mathematical expression “x^y”. The general syntax is as:

math.pow(x, y)

**Example 11:**

import math x = 2 y = 3 # Calculate x raised to the power of y. result = math.pow(x, y) # Display the result. print(x, "raised to the power of", y, "is:", result)

Output: 2 raised to the power of 3 is: 8.0

In this example, we have imported the math module, which provides various mathematical functions. Then, we have defined x = 2 and y = 3. We have used the math.pow(x, y) function to calculate “x” raised to the power of “y”, and store the result in the variable result. At last, we printed the result to see the output, which in this case would be 8, as 2^3 equals 8.

**8. sqrt(x):** This function of math module returns the square root of numeric value x. The general syntax is as:

math.sqrt(x)

**Example 12:**

import math x = 25 # Calculate the square root of x result = math.sqrt(x) # Display the result print("The square root of", x, "is:", result)

Output: The square root of 25 is: 5.0

In this example, we have first imported the math module. Then, we have defined x = 25. We have used the math.sqrt(x) function to calculate the square root of “x”, and store the result in the variable result. At last, we printed the result to see the output, which in this case would be 5, as the square root of 25 is 5.

In this tutorial, we have discussed the **math module in Python** with lots of various examples. Hope that you will have understood the basic concepts of importing math module and enjoyed this tutorial.

Thanks for reading!!!