List of Python Math Modules & Functions

List of Python Math Modules & Functions

In this blog, we will discuss the math module in Python and its various functions.

Introduction

The math module is a built-in Python library that provides mathematical functions and constants. It is a powerful tool for performing mathematical operations and can be used in a variety of applications. This article covers the following topics: an overview of the Python math module and its purpose and usage and the constants module in Python. The various mathematical operations in Python, the representation of functions in Python, and a brief description of the functions in the math module. Let’s begin exploring these concepts.

Python math modules

Python includes multiple built-in modules with math-related functions and constants. These modules are useful for tasks such as calculating statistical data, performing complex mathematical operations, and working with geometric shapes. The math and NumPy modules are some common math modules in Python.

In addition to the functions and constants provided by the math module, Python also includes a number of other math-related modules, such as the cmath module for complex math operations and the decimal module for high-precision decimal arithmetic.

What is Math Module in Python?

To use the math module, which is a built-in Python module that offers various mathematical functions and constants, you must first import it using the following syntax:

import mathCode language: Python (python)

After importing the math module, you can access its functions and constants using the math.function_name() syntax. Such as, using the sqrt() function for calculating square root, you would write math.sqrt(number).

A module on constants in mathematics

The constants module is a Python module that provides access to a variety of mathematical constants, such as pi and e. To use the constants module, you must first import it using the following syntax:

from constants import *Code language: Python (python)

Once you have imported the constants module, you can access its constants using the constant_name syntax. For example, to use the value of pi, you would write pi.

import constant

print(constant.PI) Code language: Python (python)

Output

3.14Code language: Python (python)

Math Module: Mathematical Operations

Python has several built-in functions for performing common mathematical operations, such as addition, subtraction and division. Here are a few examples:

sum = 1 + 2
difference = 2 - 1
product = 3 * 4
quotient = 4 / 2
print(sum, difference, product, quotient)Code language: Python (python)

Output

3 1 12 2Code language: Python (python)

In addition to these basic operations, Python also provides several more advanced mathematical functions, such as square roots, logarithms, and trigonometric functions. You can find these functions in the math module and other math-related modules.

Representation of Functions in Python

In Python, a function is a standalone block of code that performs a specific task and can be run multiple times from various parts of your code. You can define a function in Python using the def keyword, followed by the function name and a set of parentheses that may contain one or more parameters. For example:

def greet(name):
  print("Hi, " + name)Code language: Python (python)

To run a function in Python, you use the function name with some necessary arguments inside a set of parentheses. Such as:

print(greet("Reader"))Code language: Python (python)

Output

Hi ReaderCode language: Python (python)

Defining functions and doing calculations are examples of computational logic (i.e., they tell computers what should happen). But, the difference between them is functions allow us to define how exactly things happen by giving them names and parameters. so that our programs can call them later when necessary. This makes them very useful because they don’t need much memory space compared with other algorithms like loops!

Python Math Module Functions Description 

The math module in Python provides various functions that allow you to perform mathematical operations. Let me show you now some of the generally used math functions in Python:

  • abs(x): Returns the absolute value (i.e., an unsigned integer that is the positive or negative value that results when x is added to itself)
  • ceil(x): Returns the smallest whole number that is greater than or equal to the input number x.
  • floor(x): Returns the largest whole number that is less than or equal to the input number x.
  • exp(x): Returns the value of e^x, where e is the base of the natural logarithm (approximately 2.718).
  • log(x): Returns the natural logarithm of the input number x.
  • log10(x): it will produce the base-10 logarithm of the input number x as result.
  • pow(x, y): Returns the value of x to the power of y.
  • sqrt(x): Returns the square root of the input number x.
  • cos(x): This function returns the cosine of the input number x, which is measured in radians.
  • sin(x): Returns the sine of the input number x, which is measured in radians.
  • tan(x): Returns the tangent of the input number x, which is measured in radians.

To use these functions, you will need to import the math module in your Python code using the following statement:

import math

a = math.sin(math.pi / 2)
print(a)

b = math.factorial(5)
print(b)

c = math.floor(12.6567)
print(c)

d = math.ceil(12.4567)
print(d)

e = math.exp(5)
print(e)

f = math.log(5)
print(f)

g = math.log10(5)
print(g)

h = math.pow(2,6)
print(h)

i = math.sqrt(625)
print(i)

j = math.abs(-12)
print(j)Code language: Python (python)

Output

1.0
120
12
13
148.4131591025766
1.6094379124341003
0.6989700043360189
64.0
25.0
12Code language: Python (python)

Some Specific Functions

Some more specific functions in Python are gcd() and perm().

The gcd() function calculates the greatest common divisor (GCD) of two integers. It is a built-in function in Python. The GCD of two integers is the largest integer that can divide both of them without a remainder.

Here is an example of how to use the gcd() function in Python:

import math

p = 15
q = 20

gcd = math.gcd(p, q)
print(gcd)  Code language: Python (python)

Output

5Code language: Python (python)

The perm() function is a built-in function in Python that calculates the number of permutations of a given set of objects. A permutation is a specific arrangement of a group of items in a particular order.

An example of how to use the perm() function in Python:

import math

n = 5
r = 2

num_permutations = math.perm(n, r)
print(num_permutations)  Code language: Python (python)

Output

20Code language: Python (python)

A Factorial is a function that returns the product of all integers from 1 to a given number. It means, if you have an integer n, then n! is the product of all its factors up to and including n. For example, if you need to calculate 5! then this function will return 120 as output.

f(n) = n! * f(n – 1)

import math

print(math.factorial(5))Code language: Python (python)

Output

120Code language: Python (python)

Conclusion

To summarize, the math module in Python provides a variety of functions for performing different mathematical operations. These functions include finding the smallest integer greater than or equal to a number, the largest integer less than or equal to a number, the value of e^x, the natural logarithm of a number, the base-10 logarithm of a number, the value of x raised to the power of y, the square root of a number, and the trigonometric functions of sine, cosine, and tangent. These functions are useful in various applications, such as scientific and engineering calculations, data analysis, and more. To use these functions, you must import the math module into your Python code.

Thus, the math module in Python is a valuable resource for performing mathematical operations and accessing mathematical constants. Whether you are a beginner or an experienced Python programmer, the math module can be a useful addition to your toolkit. We hope that this blog has provided a helpful overview of the math module and its functions. You are now ready to start using these tools in your own Python projects.

Sharing is caring

Did you like what Srishti Kumari wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far