Programs

Python Math Module & Functions

Mathematical calculations and conversions are extremely important when it comes to programming and software development. Unlike languages like C, Java, and such, wherein programmers were required to code the mathematical function they required to perform, Python comes with an inbuilt Math module with a number of functions at the disposal of the programmer. This module comes in extremely handy, whether you’re working on a scientific project, fintech app, or any programming endeavor at all – you can’t escape the need for mathematical operations.

For straightforward mathematical operations, Python allows simple operators such as –

  • + for addition
  • – for subtraction
  • * for multiplication, and
  • / for division.

However, you might require more complex mathematical calculations beyond just performing basic operations. That is where the Python Math Module comes to the fore. These advanced operations include operations such as logarithm, exponents, power, etc. The benefit of the Math module is that by simply importing the module, the programmer gets access to all the functions in the module, and they can invoke the function with a simple command and get the calculations done. This convenience makes the Math module extremely important – and that is why we will look at that module in-depth in this article.

We’ll look at some important functions and their uses, and will end the article with a comprehensive table of all Math module functions provided by the Python programming language.

Check out our data science training to upskill yourself

Math Module in Python Programming Language

The Math module in Python comes pre-defined with some of the most useful mathematical functions. These include trigonometric functions, logarithmic functions, representation functions, angle conversion, and so much more. Additionally, the Math module also defines two additional constants.

Pi is a well-known mathematical constant. Defined as the ratio of circumference to the diameter of the circle, the value of the constant ‘pi’ is 3.141592653589793.

Here’s an example to try out!

>>> import math

>>>math.pi

3.141592653589793

The second constant defined in the Math module is ‘e’. Known as Euler’s number, ‘e’ is the base of the natural log. The value of the constant ‘e’ is 2.718281828459045. Try the below code to see it for yourself.

Check our US - Data Science Programs

>>> import math

>>> math.e

2.718281828459045

As mentioned earlier, the Math module in Python also contains functions for calculating the trigonometric ratios for any given angle. The functions – separate functions for separate trigonometric functions (sin, cos, tan, etc.) – take the value of the angle in radian as the argument. In general, however, we use degrees to express angles. That is where another function of the Math module comes in handy. The Math module gives programmers two angle conversion functions: degrees() and radians(), to convert the angle from degrees to radians and vice versa.

Check the below example that converts the angle of 60degree to radians and vice versa. (Please note that π radians are equivalent to 180 degrees).

>>> import math

>>> math.radians(60)

0.5235987755982988

>>> math.degrees(math.pi/3)

59.999999999999996

In the below examples, we look at the sin, cos, and tan ratios for the 60 degree angle:

>>> math.sin(0.5235987755982988)

0.49999999999999994

>>> math.cos(0.5235987755982988)

0.8660254037844387

>>> math.tan(0.5235987755982988)

0.5773502691896257

Let’s look at some other, equally important functions, provided in the Math module in Python programming language:

math.log()

This function returns the natural logarithm of any given number. As mentioned earlier, the natural logarithm refers to the base ‘e’. So, math.log() calculates log to base ‘e’.

Here’s an example using the math.log() function:

>>> import math

>>>math.log(100)

4.60517

math.log10()

This method returns the logarithm of the given number in the base 10. This is also known as the standard logarithm, as opposed to the previous natural logarithm. Here’s an example for that:

>>> import math

>>>math.log10(100)

2.0

math.exp()

This method returns a float number after raising the given number to the power of e. In other words, this function returns the value of e**x. An example of this function:

>>> import math

>>>math.exp(100)

2.70481

Read our Popular US - Data Science Articles

math.pow()

This function receives two arguments – both of which are of float data type. Then, the function raises the first number to the power of the second number, and returns the result. So, pow(3,2) is essentially 3^2, or 3**2. Example:

>>> import math

>>> math.pow(2,3)

8.0

math.sqrt()

This function simply returns the square root of the given number. This takes in a float value as an argument and returns float, too. Example:

>>> import math

>>> math.sqrt(64)

8.0

Representation functions

Apart from the functions discussed above, the Math module in Python also provides various representation functions – which include the ceiling and floor functions. The ceil() function takes an argument and approximates it to the smallest integer greater than or equal to the given argument. The floor() function, on the other hand, returns the largest integer less than or equal to the given number. Examples of the ceiling and floor functions include –

>>> import math

>>> math.ceil(3.5867)

4            

>>> math.floor(5.5687)

5

A comprehensive list of all Math module functions

So far, you have got an understanding of how to work with the Math module and its various functions. Now, here’s a comprehensive table for you, listing all the Math module functions and their uses. Feel free to bookmark this article and return to this table whenever needed!

 

Function Description
ceil(x) Returns the smallest integer greater than or equal to x.
copysign(x, y) Returns x with the sign of y
fabs(x) Returns the absolute value of x
factorial(x) Returns the factorial of x
floor(x) Returns the largest integer less than or equal to x
fmod(x, y) Returns the remainder when x is divided by y
frexp(x) Returns the mantissa and exponent of x as the pair (m, e)
fsum(iterable) Returns an accurate floating point sum of values in the iterable
isfinite(x) Returns True if x is neither an infinity nor a NaN (Not a Number)
isinf(x) Returns True if x is a positive or negative infinity
isnan(x) Returns True if x is a NaN
ldexp(x, i) Returns x * (2**i)
modf(x) Returns the fractional and integer parts of x
trunc(x) Returns the truncated integer value of x
exp(x) Returns e**x
expm1(x) Returns e**x – 1
log(x[, b]) Returns the logarithm of x to the base b (defaults to e)
log1p(x) Returns the natural logarithm of 1+x
log2(x) Returns the base-2 logarithm of x
log10(x) Returns the base-10 logarithm of x
pow(x, y) Returns x raised to the power y
sqrt(x) Returns the square root of x
acos(x) Returns the arc cosine of x
asin(x) Returns the arc sine of x
atan(x) Returns the arc tangent of x
atan2(y, x) Returns atan(y / x)
cos(x) Returns the cosine of x
hypot(x, y) Returns the Euclidean norm, sqrt(x*x + y*y)
sin(x) Returns the sine of x
tan(x) Returns the tangent of x
degrees(x) Converts angle x from radians to degrees
radians(x) Converts angle x from degrees to radians
acosh(x) Returns the inverse hyperbolic cosine of x
asinh(x) Returns the inverse hyperbolic sine of x
atanh(x) Returns the inverse hyperbolic tangent of x
cosh(x) Returns the hyperbolic cosine of x
sinh(x) Returns the hyperbolic cosine of x
tanh(x) Returns the hyperbolic tangent of x
erf(x) Returns the error function at x
erfc(x) Returns the complementary error function at x
gamma(x) Returns the Gamma function at x
lgamma(x) Returns the natural logarithm of the absolute value of the Gamma function at x
pi Mathematical constant, the ratio of circumference of a circle to it’s diameter (3.14159…)
e mathematical constant e (2.71828…)

In conclusion

Python is an extremely versatile language because of the number of modules it offers. This is especially true when it comes to data science and analysis. Further, Python is an extremely easy language to learn and master if you’re a beginner. At upGrad, we’ve guided and mentored the progress journey of many passionate individuals – including both freshers and experienced professionals. Professional Certificate Program in Data Science and Business Analytics is one such attempt of upGrad to bring you closer to the power of data science by imparting knowledge of all essential tools and libraries.

1. What is the Math module in Python?

Math module is provided in Python programming language to help programmers perform complex mathematical operations without writing the code for it from scratch.

2. Is the Math module the only way to perform mathematical operations in Python?

No, simple mathematical operations can be performed using the basic operators. However, when it comes to more complex operations, the Math module is the more sensible way to go about it - instead of writing the entire code from scratch!

3. How many functions are there in the Math module in Python?

Please scroll up for the comprehensive list of all functions available in the Math module.

Want to share this article?

Leave a comment

Your email address will not be published. Required fields are marked *

Our Best Data Science Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×