Menu Close

Python Math Module

Python math Module

In this article, we are going to learn all about the Python math module along with their methods using examples.
math function in Python provides lots of functions to perform mathematical operations.

In this python math, we will go through all the Python math module’s methods along with examples.
To understand this example, you would have basic knowledge of Python programming.

Python math module

Python math module is used to perform the mathematic calculations.
math module in python has several functions available that make mathematical calculation easy.
To use the math module you mush have to import the math module using the import keyword.

Attributes of math module in Python

To get the all the attributes of Python math module, use dir function.

import math
x = dir(math)
print(x)

Output

['__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']

Constants

math module in python provide some constants.

import math
print("math.pi:- ", math.pi)
print("math.e:- ", math.e)
print("math.tau:- ", math.tau)

#A floating point positive infinity
print("math.e:- ", math.e)

#A floating-point "not a number" (NaN) value.
print("math.nan:- ", math.nan)

Output

math.pi:-  3.141592653589793
math.e:-  2.718281828459045
math.tau:-  6.283185307179586
math.e:-  2.718281828459045
math.nan:-  nan

Number-theoretic and representation function

math module in Python provides lots of functions to perform an operation using numbers.

math.ceil()

ceil() function is used to get the ceiling value to the given number.

import math
x = math.ceil(3.6)
print(x)

Output will be:- 4

math.floor()

floor() function is used to get the floor value to the given number.

import math
x = math.floor(2.3)
print(x)

Output will be:- 2

math.fabs(x)

python math fabs function return the absolute value of x.

import math
print(math.fabs(-120))

Output will be:- 120

math.factorial(x)

python math factorial function return the absolute value of x.

import math
print(math.factorial(5))

Output will be:- 120

math.gcd(*number)

Python math gcd function Return the greatest common divisor of the specified integer arguments.

import math
print(math.gcd(12, 8))

Output will be:- 4

math.lcm(*number)

Python math lcm function Return the least common multiple of the specified integer arguments.

import math
print(math.lcm(12, 8))

Output will be:- 24

math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)

The math.isclose() method checks whether two values are close to each other, or not. Returns True if the values are close, otherwise False.

import math
print(math.isclose(1.23, 1.23))
print(math.isclose(1.23, 1.25))

Output

True
False

math.isfinite(x)

The Python math isfinite() method Return True if x is neither an infinity nor a NaN, and False otherwise.

import math
print(math.isfinite(12))
print(math.isfinite(-12))
print(math.isfinite(math.inf))

Output

True
True
False

math.fmod(x, y)

Python math.fmod() method returns the remainder of x/y.

import math
print(math.fmod(13, 4))

Output will be:- 1

math.frexp(x)

Python math frexp() method returns the mantissa and exponent of the specified number as a pair of (m, e)

import math
print(math.frexp(5))

Output will be:- (0.625, 3)

math.fsum(iterable)

Python math fsum() method returns the sum of all the items of specified iterable (list, set, tuple)

import math
my_list = [12, 23, 45, 56, 19, 10]
print(math.fsum(my_list))

Power and logarithmic function

Python math module provides lots of function to perform power and logarithmic operations.

math.pow()

The math.pow() method takes two float arguments, raises the first to the second, and returns the result.

import math
x = math.pow(2, 4)
print(x)

Output will be:- 16.0

math.sqrt()

The math.sqrt() method return the square root of given number.

import math
x = math.sqrt(25)
print(x)

Output will be:- 5

math.log(x)

The math.log() method returns the natural logarithm of a given number.

import math

x = math.log(25)
print(x)

Output will be:- 3.2188758248682006

math.log2()

The math.log2() method returns base-2 logarithm of a given number.

import math
x = math.log2(25)
print(x)

Output will be:- 4.643856189774724

math.log10()

The math.log10() method returns the base-10 logarithm of the given number.

import math
x = math.log10(10)
print(x)

Output will be:- 1.0

Trigonometric function

You can also use Trigonometric function. such as sin(), cos(), tan(), acos(), asin(), atan(), etc

import math
a = 45
b = 0.999

#Return the arc cosin of x, value of x should be -1 <= x <= 1.
print(f"acos({b}):- {math.acos(b)}")

#Return the arc sin of x, value of x should be -1 <= x <= 1.
print(f"atan({b}):- {math.atan(b)}")

#Return the arc tangent of x, value of x should be -1 <= x <= 1.
print(f"asin({b}):- {math.asin(b)}")

print('--------------')
#Return the cos of x.
print(f"cos({a}):- {math.cos(a)}")

#Return the sin of x.
print(f"sin({a}):- {math.sin(a)}")

#Return the arc tan.
print(f"tan({a}):- {math.tan(a)}")

Output

acos(0.999):- 0.044725087168733454
atan(0.999):- 0.784897913314115
asin(0.999):- 1.5260712396261633
--------------
cos(45):- 0.5253219888177297
sin(45):- 0.8509035245341184
tan(45):- 1.6197751905438615

Angular Conversion

You can convert degree to radians and vice-verca.

math.degrees(x)

Python math degrees function is used to convert angle x from radians to degree.

import math
a = 45
print(f"degrees({a}):- {math.degrees(a)}")

Output will be:- degrees(45):- 2578.3100780887044

math.radians(x)

Python math radians function is used to convert angle x from degree to radians.

import math
a = 2578.3100780887044
print(f"radians({a}):- {math.radians(a)}")

Output will be:- radians(2578.3100780887044):- 45.0

Hyperbolic Function

Hyperbolic functions are analogs of trigonometric functions that are based on hyperbolas instead of circles.

import math

a = 45
#Return the inverse hyperbolic of cosine of a
print(f"acosh({a}):- {math.acosh(a)}")

#Return the inverse hyperbolic sine of a
print(f"asinh({a}):- {math.asinh(a)}")

#Return the inverse hyperbolic tangent of a
x = 0.90 # x must be -1 < x > 1
print(f"atanh({x}):- {math.atanh(x)}")

print('-----------------------')


#Return the inverse hyperbolic cosine of a
print(f"cosh({a}):- {math.cosh(a)}")

#Return the inverse hyperbolic cosine of a
print(f"sinh({a}):- {math.sinh(a)}")

#Return the inverse hyperbolic cosine of a
print(f"tanh({a}):- {math.tanh(a)}")

Output

acosh(45):- 4.499686190671499
asinh(45):- 4.49993310426429
atanh(0.9):- 1.4722194895832204
-----------------------
cosh(45):- 1.7467135528742547e+19
sinh(45):- 1.7467135528742547e+19
tanh(45):- 1.0

Conclusion

In this Python math module tutorial, you have learned all about the Python math module along with its methods.
python math module provides lots of function that helps us to perform mathematical operations.

If you like this article, please share keep visiting for further Python bult-in modules tutorials.

Python built-in modules


For more information about Python math module:- Click Here

Python Datetime Module Tutorial

Related Posts