Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Science USbreadcumb forward arrow iconPython Math Module & Functions

Python Math Module & Functions

Last updated:
28th Sep, 2022
Views
Read Time
8 Mins
share image icon
In this article
Chevron in toc
View All
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.

Ads of upGrad blog

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

Ads of upGrad blog

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!

 

FunctionDescription
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
piMathematical constant, the ratio of circumference of a circle to it’s diameter (3.14159…)
emathematical 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.

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.
Get Free Consultation

Select Coursecaret down icon
Selectcaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Best Data Science Courses

Frequently Asked Questions (FAQs)

11. 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.

22. 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!

33. 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.

Explore Free Courses

Suggested Blogs

Top 10 Real-Time SQL Project Ideas: For Beginners & Advanced
14863
Thanks to the big data revolution, the modern business world collects and analyzes millions of bytes of data every day. However, regardless of the bus
Read More

by Pavan Vadapalli

28 Aug 2023

Python Free Online Course with Certification [US 2024]
5519
Data Science is now considered to be the future of technology. With its rapid emergence and innovation, the career prospects of this course are increa
Read More

by Pavan Vadapalli

14 Apr 2023

13 Exciting Data Science Project Ideas & Topics for Beginners in US [2024]
5474
Data Science projects are great for practicing and inheriting new data analysis skills to stay ahead of the competition and gain valuable experience.
Read More

by Rohit Sharma

07 Apr 2023

4 Types of Data: Nominal, Ordinal, Discrete, Continuous
6068
Data refers to the collection of information that is gathered and translated for specific purposes. With over 2.5 quintillion data being produced ever
Read More

by Rohit Sharma

06 Apr 2023

Best Python Free Online Course with Certification You Should Check Out [2024]
5628
Data Science is now considered to be the future of technology. With its rapid emergence and innovation, the career prospects of this course are increa
Read More

by Rohit Sharma

05 Apr 2023

5 Types of Binary Tree in Data Structure Explained
5385
A binary tree is a non-linear tree data structure that contains each node with a maximum of 2 children. The binary name suggests the number 2, so any
Read More

by Rohit Sharma

03 Apr 2023

42 Exciting Python Project Ideas & Topics for Beginners [2024]
5857
Python is an interpreted, high-level, object-oriented programming language and is prominently ranked as one of the top 5 most famous programming langu
Read More

by Rohit Sharma

02 Apr 2023

5 Reasons Why Python Continues To Be The Top Programming Language
5339
Introduction Python is an all-purpose high-end scripting language for programmers, which is easy to understand and replicate. It has a massive base o
Read More

by Rohit Sharma

01 Apr 2023

Why Should One Start Python Coding in Today’s World?
5220
Python is the world’s most popular programming language, used by both professional engineer developers and non-designers. It is a highly demanded lang
Read More

by Rohit Sharma

16 Feb 2023

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon