Python Mathematical Functions

PythonProgrammingServer Side Programming

The math module is used to access mathematical functions in the Python. All methods of this functions are used for integer or real type objects, not for complex numbers.

To use this module, we should import that module into our code.

import math

Some Constants

These constants are used to put them into our calculations.

Sr.No. Constants & Description
1

pi

Return the value of pi: 3.141592

2

E

Return the value of natural base e. e is 0.718282

3

tau

Returns the value of tau. tau = 6.283185

4

inf

Returns the infinite

5

nan

Not a number type.

Numbers and Numeric Representation

These functions are used to represent numbers in different forms. The methods are like below −

Sr.No. Function & Description
1

ceil(x)

Return the Ceiling value. It is the smallest integer, greater or equal to the number x.

2

copysign(x, y)

It returns the number x and copy the sign of y to x.

3

fabs(x)

Returns the absolute value of x.

4

factorial(x)

Returns factorial of x. where x ≥ 0

5

floor(x)

Return the Floor value. It is the largest integer, less or equal to the number x.

6

fsum(iterable)

Find sum of the elements in an iterable object

7

gcd(x, y)

Returns the Greatest Common Divisor of x and y

8

isfinite(x)

Checks whether x is neither an infinity nor nan.

9

isinf(x)

Checks whether x is infinity

10

isnan(x)

Checks whether x is not a number.

11

remainder(x, y)

Find remainder after dividing x by y.

Example Code

import math
print('The Floor and Ceiling value of 23.56 are: ' + str(math.ceil(23.56)) + ', ' + str(math.floor(23.56)))
x = 10
y = -15
print('The value of x after copying the sign from y is: ' + str(math.copysign(x, y)))
print('Absolute value of -96 and 56 are: ' + str(math.fabs(-96)) + ', ' + str(math.fabs(56)))
my_list = [12, 4.25, 89, 3.02, -65.23, -7.2, 6.3]
print('Sum of the elements of the list: ' + str(math.fsum(my_list)))
print('The GCD of 24 and 56 : ' + str(math.gcd(24, 56)))
x = float('nan')
if math.isnan(x):
    print('It is not a number')
x = float('inf')
y = 45
if math.isinf(x):
    print('It is Infinity')
print(math.isfinite(x)) #x is not a finite number
print(math.isfinite(y)) #y is a finite number

Output

The Floor and Ceiling value of 23.56 are: 24, 23
The value of x after copying the sign from y is: -10.0
Absolute value of -96 and 56 are: 96.0, 56.0
Sum of the elements of the list: 42.13999999999999
The GCD of 24 and 56 : 8
It is not a number
It is Infinity
False
True

Power and Logarithmic Functions

These functions are used to calculate different power related and logarithmic related tasks.

Sr.No. Function & Description
1

pow(x, y)

Return the x to the power y value.

2

sqrt(x)

Finds the square root of x

3

exp(x)

Finds xe, where e = 2.718281

4

log(x[, base])

Returns the Log of x, where base is given. The default base is e

5

log2(x)

Returns the Log of x, where base is 2

6

log10(x)

Returns the Log of x, where base is 10

Example Code

Live Demo

import math
print('The value of 5^8: ' + str(math.pow(5, 8)))
print('Square root of 400: ' + str(math.sqrt(400)))
print('The value of 5^e: ' + str(math.exp(5)))
print('The value of Log(625), base 5: ' + str(math.log(625, 5)))
print('The value of Log(1024), base 2: ' + str(math.log2(1024)))
print('The value of Log(1024), base 10: ' + str(math.log10(1024)))

Output

The value of 5^8: 390625.0
Square root of 400: 20.0
The value of 5^e: 148.4131591025766
The value of Log(625), base 5: 4.0
The value of Log(1024), base 2: 10.0
The value of Log(1024), base 10: 3.010299956639812

Trigonometric & Angular Conversion Functions

These functions are used to calculate different trigonometric operations.

Sr.No. Function & Description
1

sin(x)

Return the sine of x in radians

2

cos(x)

Return the cosine of x in radians

3

tan(x)

Return the tangent of x in radians

4

asin(x)

This is the inverse operation of the sine, there are acos, atan also.

5

degrees(x)

Convert angle x from radian to degrees

6

radians(x)

Convert angle x from degrees to radian

Example Code

Live Demo

import math
print('The value of Sin(60 degree): ' + str(math.sin(math.radians(60))))
print('The value of cos(pi): ' + str(math.cos(math.pi)))
print('The value of tan(90 degree): ' + str(math.tan(math.pi/2)))
print('The angle of sin(0.8660254037844386): ' + str(math.degrees(math.asin(0.8660254037844386))))

Output

The value of Sin(60 degree): 0.8660254037844386
The value of cos(pi): -1.0
The value of tan(90 degree): 1.633123935319537e+16
The angle of sin(0.8660254037844386): 59.99999999999999
raja
Published on 31-Oct-2018 16:29:37
Advertisements