Javatpoint Logo
Javatpoint Logo

Python Math Module

Introduction

In this article, we are discussing Math Module in Python. We can easily calculate many mathematical calculations in Python using the Math module. Mathematical calculations may occasionally be required when dealing with specific fiscal or rigorous scientific tasks. Python has a math module that can handle these complex calculations. The functions in the math module can perform simple mathematical calculations like addition (+) and subtraction (-) and advanced mathematical calculations like trigonometric operations and logarithmic operations.

This tutorial teaches us about applying the math module from fundamentals to more advanced concepts with the support of easy examples to understand the concepts fully. We have included the list of all built-in functions defined in this module for better understanding.

What is Math Module in Python?

Python has a built-in math module. It is a standard module, so we do not need to install it separately. We only must import it into the program we want to use. We can import the module, like any other module of Python, using import math to implement the functions to perform mathematical operations.

Since the source code of this module is in the C language, it provides access to the functionalities of the underlying C library. Here we have given some basic examples of the Math module in Python. The examples are written below -

Program Code 1:

Here we give an example of a math module in Python for calculating the square root of a number. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

3.0

This Python module does not accept complex data types. The more complicated equivalent is the cmath module.

We can, for example, calculate all trigonometric ratios for any given angle using the built-in functions in the math module. We must provide angles in radians to these trigonometric functions (sin, cos, tan, etc.). However, we are accustomed to measuring angles in terms of degrees. The math module provides two methods to convert angles from radians to degrees and vice versa.

Program code 2:

Here we give an example of a math module in Python for calculating the factorial of a number. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

5
120

Program code 3:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

12
2
144.0

Program code 4:

Here we give an example of a math module in Python for calculating the GCD of two numbers. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

12
4
4

Math Module:

Program code-

We use a program code to know all the functions of the Math module in Python. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

['__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'acos',
 'acosh',
 'asin',
 'asinh',
 'atan',
 'atan2',
 'atanh',
 'ceil',
 'comb',
 'copysign',
 'cos',
 'cosh',
 'degrees',
 'dist',
 'e',
 'erf',
 'erfc',
 'exp',
 'expm1',
 'fabs',
 'factorial',
 'floor',
 'fmod',
 'frexp',
 'fsum',
 'gamma',
 'gcd',
 'hypot',
 'inf',
 'isclose',
 'isfinite',
 'isinf',
 'isnan',
 'isqrt',
 'ldexp',
 'lgamma',
 'log',
 'log10',
 'log1p',
 'log2',
 'modf',
 'nan',
 'perm',
 'pi',
 'pow',
 'prod',
 'radians',
 'remainder',
 'sin',
 'sinh',
 'sqrt',
 'tan',
 'tanh',
 'tau',
 'trunc']

Now we discuss the functionalities mentioned above in Python.

1. Ceil() :

In the case of the ceiling, it will always return the nearest integer value toward positive. Here we give the program code of ceil() in Python. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

12.7856
13

2. floor() :

In the case of the floor(), it will always return the nearest integer value toward negative. Here we give the program code of floor() in Python. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

12.7856
13

3. fabs() :

In the case of fabs(), it will always return the nearest value towards negative. Here we give the program code of fabs() in Python. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

12.7856
13

How to print the value of pi?

We write code to print the pi value in the Python math module. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

3.141592653589793

What is the division concept in Python math module?

We write code for the division concept in the Python math module. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

12
5
Float Division 2.4
Integer Division 2
Reminder 2

Constants in Math Module

The value of numerous constants, including pi and tau, is provided in the math module so that we do not have to remember them. Using these constants eliminates the need to write down the value of each constant precisely and repeatedly. The math module includes the following constants:

  1. Euler's Number
  2. Tau
  3. Infinity
  4. Pi
  5. Not a Number (NaN)

Let's go over each of them one by one.

Euler's Number

The value 2.71828182845 of Euler's number is returned by the math.e constant.

Syntax of this is:

The syntax of Euler's number is given below -

Program Code:

Now we write a code for print the value of Euler's number. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

The value of Euler's Number is:  2.718281828459045

Tau

The tau is the ratio of a circle's circumference to its radius. The value tau returned by the tau constant is 6.283185307179586.

Syntax of this is:

The syntax of tau is given below -

Program Code

Now we write a code to print the value of tau. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

The value of Tau is:  6.283185307179586

Infinity

Infinity means which have no boundaries. Infinity refers to anything limitless or never-ending in both directions of the actual number line. Numbers cannot adequately represent it. The math.inf returns a positive infinity constant. We can use -math.inf to print negative infinity. Here we can also compare a very large floating-point number with infinity values which are positive and negative.

Syntax of this is:

The syntax of infinity is given below -

Program Code 1:

Now we write a code to know whether the value is infinity or not. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

inf
-inf

Program code 2:

Now we write a code to know whether the value is infinity. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

True
True

Pi

Pi is a constant value in mathematics and is used to find the radius of a circle. Pi is known to everyone. It is mathematically represented as the fraction 22/7 or the decimal number 3.14. math.pi gives the most accurate value of pi.

Syntax of this is:

The syntax of pi is given below -

Program Code 1:

Now we write a code to print the value of pi. The code is given below -

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Output:

The value of pi is  3.141592653589793

Let us calculate the circumference of a circle.

Program Code 2:

Now we write a code for the value of circle circumference using the pi function in Python. The code is given below -

Output

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

The value of the circle circumference: 25.132741228718345

NaN

The math.nan gives us a floating-point nan (Not a Number) value. This amount is not a valid numeric value. Float("nan") and the nan constant are comparable.

Program Code:

Now we write a code for now the floating-point nan (Not a Number) value. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

nan

Mathematical Operations with Math Module

The functions that are required in representation theory and number theory, such as calculating the factorial of an integer, will be covered in this part.

Calculating the Ceiling and the Floor Value

The terms "ceiling value" and "floor value" refer to the smallest integral value larger than the number and the largest integral value less than the number, respectively. The ceil() and floor() methods simplify calculating this.

Program Code:

Here we write a code in Python using floor() and ceil() functions. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

The ceiling value of 4.346 is : 5
The floor value of 4.346 is : 4

Calculating the Factorial of the Number

We may determine the factorial of a given integer in a one-liner code by using the math.factorial() function. The Python interpreter will send a message if the given number is not integral.

Code

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

The factorial of 6 is :  720
Cannot calculate factorial of a non-integral number

Calculating the Absolute Value

The method math.fabs() returns the absolute number of the number given to the function.

Code

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

The absolute value of -45 is:  45.0

Calculating the Exponential

x to the power of e, often known as the exponential of a number x, is calculated using the exp() function.

Code

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

The exponenetial value of 4 is:  54.598150033144236
The exponenetial value of -3 is:  0.049787068367863944
The exponenetial value of 0.0 is:  1.0

Calculating the Power of a Number

x**y is computed via the pow() function. This function calculates the value of the power after converting its inputs into floats.

Code

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

The value of 4 to the power of 5 is:  1024.0

Without using of pow function, we can also calculate Power in Python. The code is given below -

Program Code:

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

The value of 4 to the power of 5 is:  1024

Calculating Sine, Cosine, and Tangent

The values of sine, cosine, and tangent of an angle, which are supplied as an input to the function, are returned by the sin(), cos(), and tan() methods. This function expects a value that is provided in radians.

Code

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

The sine of pi/4 is :  0.7071067811865475
The cosine of pi/4 is :  0.7071067811865476
The tangent of pi/4 is :  0.9999999999999999

The dir( ) Function

A sorted list of strings comprising the identifiers of the functions defined by a module is what the built-in method dir() delivers.

The list includes the names of modules, each specified constants, functions, and methods. Here is a straightforward illustration:

Code

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

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

Description of all the Functions in Python Math Module

Here is a list of all the properties and functions specified in the math module, along with a brief description of what each one does.

Function Description
ceil(x) The lowest integer bigger than or equal to x is returned.
copysign(x, y) gives x back with the sign of y.
fabs(x) gives x's absolute value back.
factorial(x) provides the x factorial back.
floor(x) gives back the biggest integer that is less than or equal to x.
fmod(x, y) returns the leftover value after dividing x by y.
frexp(x) returns the pair of the mantissa and exponent of x. (m, e)
fsum(iterable) returns the iterable's correct floating point sum of all values.
isfinite(x) If x is neither an infinity nor a NaN, it returns True (Not a Number)
isinf(x) If x is a positive or negative infinity, it returns True.
isnan(x) If x is a NaN, it returns True.
ldexp(x, i) gives back x * (2**i).
modf(x) gives x's fractional and integer components back.
trunc(x) x's shortened integer value is returned.
exp(x) delivers e**x
expm1(x) yields e**x - 1
log(x[, b]) gives back the x logarithm in base b. (defaults to e)
log1p(x) the natural logarithm of 1 + x is returned.
log2(x) gives x's base-2 logarithm back.
log10(x) provides x's base-10 logarithm.
pow(x, y) gives x raised to the power of y back.
sqrt(x) gives x's square root back.
acos(x) gives the arc cosine of x back.
asin(x) gives the arc sine of x back.
atan(x) gives the arc tangent of x back.
atan2(y, x) gives back atan(y / x).
cos(x) returns the x's cosine.
hypot(x, y) returns sqrt(x*x + y*y), the Euclidean norm.
sin(x) gives the sine of x back.
tan(x) gives the tangent of x back.
degrees(x) Angle x is transformed from radians to degrees.
radians(x) Angle x is transformed from degrees to radians.
acosh(x) x's inverse hyperbolic cosine is returned.
asinh(x) x's inverse hyperbolic sine is returned.
atanh(x) x's inverse hyperbolic tangent is returned.
cosh(x) gives x's hyperbolic cosine.
sinh(x) gives x's hyperbolic cosine.
tanh(x) gives x's hyperbolic tangent back.
erf(x) the error function at x is returned.
erfc(x) a function that gives the complementary error at x
gamma(x) the Gamma function at x is returned.
lgamma(x) gives the natural logarithm of the gamma function's absolute value at x.
pi The ratio of a circle's circumference to its diameter is a mathematical constant (3.14159...)
e e is a constant in mathematics (2.71828...)

Conclusion:

So, in this article, we discuss the math module in Python. In the math module, there are various functions that we can use for mathematical calculation and many more. We discuss every function in the math module and also share some program codes of these functions.


Next TopicPython Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA