Python math Functions

The Python math Library provides various Functions and Constants / Properties, which allows us to perform mathematical functionality. Unlike other Python global objects, Properties and Functions inside the Python math library object is static. So, we can access the Python math properties as math.PI and functions as math.abs(number).

Python math Object Properties

The list of Properties or Constants available in Python math library.

Python math PropertiesDescription
math.eIt returns the Euler’s Number e, approximately equal to 2.71828
math.piIt returns the Pie Value, approximately equal to 3.14
math.infThis Python math property returns the Positive Infinity. You can use -Math.INF to return the Negative Infinity.
math.nanIt returns Not A Number as output

Python math Functions

The list of mathematical Functions available in Python math Library. Please follow this links to view the tutorial on the available math functions in Python.

Python math FunctionsDescription
ceil(x)It returns the smallest integer, which is greater than or equal to the specified expression or a number.
copysign(x)This python math function finds the Absolute value of the first argument. It returns the absolute value along with the sign specified in the second argument.
fabs(x)Absolute value of a Given number
factorial(x)It finds the factorial of a specified expression or a specific number.
floor(x)Largest integer value, which is less than or equal to the specified number.
fmod(x, y)This python math function calculates the Module of the specified given arguments.
frexp(x)This function returns the mantissa and exponent of x, as pair (m, e) where m is a float value, and e is an integer value.
fsum(Iterable)Calculates and returns the sum of iterates (Tuples and Lists)
gcd(x, y)This python math function returns the greatest common divisor of two given arguments.
isclose(x, y)This function return TRUE, If the two arguments are close to each other, otherwise it returns FALSE
isfinite(x)Used to check whether the given number/expression is neither Infinity (both Positive or Negative) nor NaN. It returns TRUE If the given number is neither Infinity nor NaN (Not a Number) otherwise FALSE.
isinf(x)Checks whether the given number is Infinity (both Positive or Negative) or not. It returns TRUE If the number is Infinity otherwise FALSE
isnan(x)This python math function checks whether the given number is NaN (Not a Number) or not. It returns TRUE if the given number is NaN otherwise FALSE
round(x)It is a regular built-in function (not a Math library function). It round the specified expression or a specific number to the nearest integer.
ldexp(x, i)It returns x * (2**i). It also called inverse of Python frexp function.
modf(x)This python math function divides the given value into two arguments: Fractional Part as the first argument and integer value as the second argument.
trunc(x)It removes the decimal values from the specified expression and returns the integer value

Python Power and Logarithmic Functions

The following functions are the list of Power and logarithmic functions available in Python math Library.

Power and Logarithmic FunctionsDescription
exp(x)It calculates the power of E, Where E is Euler’s number approximately equal to 2.71828.
expm1(x)It calculates the power of E (Where E is Euler’s number approximately equal to 2.71828) and subtracts one from it.
log(x, base)This Python Power and logarithmic math function find the log value of a number with base E.
log2(x)Logarithmic value of number with base E.
log10(x)Logarithmic value of a given number with base E.
pow(x)This Python Power and logarithmic function calculate the Power of the specified expression
sqrt(x)Square root of a specified Python expression or an individual number

Python Trigonometric Functions

The following functions are the list of Trigonometric functions available in Python math Library.

Trigonometric FunctionsDescription
acos(x)It returns the Arc Cosine value of a given number
asin(x)This Python Trigonometric function returns the Arc Sine value of a given number
atan(x)Arc Tangent value of a number
atan2(y, x)It returns the angle (in radius) from the X-Axis to the specified point (y, x).
cos(x)This Python Trigonometric function returns the Cosine value of a number
hypot(x, y)It extracts the characters from a string based on the specified indices
sin(x)Sine value of a given number
tan(x)Tangent value of a given number

Python Hyperbolic Functions

The Python Hyperbolic Functions are trigonometric functions which allow us to perform the following math functions on Hyperbolic, instead of Circles.

Hybrbolic FunctionsDescription
acosh(x)It returns the Hyperbolic Arc Cosine (inverse Hyperbolic Cosine) value of a given number
asinh(x)This Python Hyperbolic math Function returns the Hyperbolic Arc Sine (inverse Hyperbolic Sine) value of a given number
atanh(x)Hyperbolic Arc Tangent (inverse Hyperbolic Tangent) value of a given number
cosh(x)Hyperbolic Cosine value of a given number
sinh(x)This Python Hyperbolic Function returns the Hyperbolic Sine value of a given number
tanh(x)Hyperbolic Tangent value of a given number

Python Angular Functions

The following functions are the list of Angular functions available in Python math Library.

Angular FunctionsDescription
degrees(x)It convert the specified angle from Radians to Degrees.
radians(x)This Python Angular function converts the specified angle from Degrees to Radians.

Python Special Functions

The following functions are the list of Special functions available in Python math Library.

Special FunctionsDescription
erf(x)It returns the error function at a specified value.
erfc(x)This Python Special function returns the complementary error function at a specified value. OR we can simply say, 1 – erf(x)
gamma(x)It returns the Gamma function at a specified value.
lgamma(x)This Python Special function returns the Natural Logarithm of the Gamma function at a specified value.

Python math Functions Examples

The following examples help you to understand these math or mathematical functions in Python

Python math constants Example

In this Python Mathematical constants example, we use the list of available constants in the math library. They are pi, e, tau, inf, and nan.

# Python Constant Examples

import math

print('pi Constant - Pi Value = ', math.pi)
print('pi Constant - Degrees of Pi Value = ', math.degrees(math.pi))

print('\ne Constant - e Value = ', math.pi)
print('e Constant - Degrees of e Value = ', math.degrees(math.e))

print('\ntau Constant - tau Value = ', math.tau)
print('tau Constant - Degrees of tau Value = ', math.degrees(math.tau))

print('\ninf Constant - Positive Infinity = ', math.inf)
print('inf Constant - Negative Infinity = ', -math.inf)

print('\nNaN Constant - Not a Number = ', math.nan)

Python math constants output

pi Constant - Pi Value =  3.141592653589793
pi Constant - Degrees of Pi Value =  180.0

e Constant - e Value =  3.141592653589793
e Constant - Degrees of e Value =  155.74607629780772

tau Constant - tau Value =  6.283185307179586
tau Constant - Degrees of tau Value =  360.0

inf Constant - Positive Infinity =  inf
inf Constant - Negative Infinity =  -inf

NaN Constant - Not a Number =  nan

Python math Functions Example 1

In this Python math or Mathematical Functions example, we are going to use the fabs to find the absolute value, and copysign to change the sign. Next, we used the Python ceil and floor function to find the Ceiling and Floor values. Within the last statement, we used the factorial function to find the factorial of a given value.

# Python Mathematical Functions Examples

import math

x = 10.98
y = 30.22
z = -40.95

print('FABS Function - Absolute Value of z = ', math.fabs(z))
print('FABS Function - Absolute Value of -124.897 = ', math.fabs(-124.897))

print('\ncopysign Function - copysign Value of x, z = ', math.copysign(x, z))
print('copysign Function - copysign Value of z, x = ', math.copysign(z, x))

print('\nCEIL Function - Ceiling Value of x = ', math.ceil(x))
print('CEIL Function - Ceiling Value of y = ', math.ceil(y))

print('\nFLOOR Function - Floor Value of x = ', math.floor(x))
print('FLOOR Function - Floor Value of y = ', math.floor(y))

print('\nFactorial Function - Factorial of 3 = ', math.factorial(3))
print('Factorial Function - Factorial of 5 = ', math.factorial(5))

Python math Functions output

FABS Function - Absolute Value of z =  40.95
FABS Function - Absolute Value of -124.897 =  124.897

copysign Function - copysign Value of x, z =  -10.98
copysign Function - copysign Value of z, x =  40.95

CEIL Function - Ceiling Value of x =  11
CEIL Function - Ceiling Value of y =  31

FLOOR Function - Floor Value of x =  10
FLOOR Function - Floor Value of y =  30

Factorial Function - Factorial of 3 =  6
Factorial Function - Factorial of 5 =  120

Python math Functions Example 2

In this Python Mathematical Functions example, we used fmod, frexp, fsum, and gcd functions with different values.

# Python mathematical Functions Examples
 
import math
 
print('FMOD Function - Mod Value of 2 and 3 = ', math.fmod(2, 3))
print('FMOD Function - Mod Value of 225.55 and 5.5 = ', math.fmod(222.55, 5.5))
 
print('\nFREXP Function - Mantissa and Exponent Value of 5 = ', math.frexp(5))
print('FREXP Function - Mantissa and Exponent Value of -9 = ', math.frexp(-9))
 
print('\nFSUM Function - Sum of Tuple Items = ', math.fsum((10, 20, 30, 40)))
print('FSUM Function - Sum of List Items = ', math.fsum([5, 22, 35, 9]))
 
print('\nGCD Function - GCD of two Value of 10 and 2 = ', math.gcd(10, 2))
print('GCD Function - GCD of two Value of 100 and 15 = ', math.gcd(100, 15))

Python math Functions output

FMOD Function - Mod Value of 2 and 3 =  2.0
FMOD Function - Mod Value of 225.55 and 5.5 =  2.5500000000000114

FREXP Function - Mantissa and Exponent Value of 5 =  (0.625, 3)
FREXP Function - Mantissa and Exponent Value of -9 =  (-0.5625, 4)

FSUM Function - Sum of Tuple Items =  100.0
FSUM Function - Sum of List Items =  71.0

GCD Function - GCD of two Value of 10 and 2 =  2
GCD Function - GCD of two Value of 100 and 15 =  5

Python Mathematical Functions Example 3

In this Python math Functions example, we used round, ldexp, mode, trunc, and remainder functions.

# Python Mathematical Functions Examples

import math

print('ROUND Function - Rounded Value 100.98763 = ', round(100.9876, 2))
print('ROUND Function - Rounded Value 125.932832 = ', round(125.932832, 3))

print('\nLDEXP Function - LDEXP (FREXP inverse) Value of 4, 5 = ', math.ldexp(4, 5))
print('LDEXP Function - LDEXP (FREXP inverse) Value of -9, 2 = ', math.ldexp(-9, 2))

print('\nMODF Function - Modf (Divided 1 to 2) Value of 100 = ', math.modf(100))
print('MODF Function - Modf (Divided 1 to 2) Value of 120.98 = ', math.modf(120.98))

print('\nTRUNC Function - Truncated Value 100.98763 = ', math.trunc(100.9876))
print('ROUND Function - Truncated Value 125.932832 = ', math.trunc(-125.932832))

print('\nREMAINDER Function - Remainder of 29 and 5 = ', math.remainder(20, 5))
print('REMAINDER Function - Remainder of 10 and 3 = ', math.remainder(10, 3))

Python Mathematical Functions output

ROUND Function - Rounded Value 100.98763 =  100.99
ROUND Function - Rounded Value 125.932832 =  125.933

LDEXP Function - LDEXP (FREXP inverse) Value of 4, 5 =  128.0
LDEXP Function - LDEXP (FREXP inverse) Value of -9, 2 =  -36.0

MODF Function - Modf (Divided 1 to 2) Value of 100 =  (0.0, 100.0)
MODF Function - Modf (Divided 1 to 2) Value of 120.98 =  (0.980000000000004, 120.0)

TRUNC Function - Truncated Value 100.98763 =  100
ROUND Function - Truncated Value 125.932832 =  -125

REMAINDER Function - Remainder of 29 and 5 =  0.0
REMAINDER Function - Remainder of 10 and 3 =  1.0

Python Logarithmic Functions Example

In this Python Logarithmic Function example, we use the exp, expm1 to get exp values. Next, we used the log, log2, and log10 functions to get the natural logarithmic value, base 2 Logarithmic value. And base 10 logarithmic value. Then we used the pow to find x raised to the power of y, and sqrt function to find the square root of a number.

# Python Power and Logarithmic Functions Examples

import math

print('EXP Function - exp Value of 5 = ', math.exp(5))
print('EXP Function - exp Value of -3 = ', math.exp(-3))

print('\nEXPM1 Function - expm1 Value of 8 = ', math.expm1(8))
print('EXPM1 Function - expm1 Value of -5 = ', math.expm1(-5))

print('\nLOG Function - logarithmic Value of 5 = ', math.log(5))
print('LOG Function - logarithmic Value of 100 Base 2 = ', math.log(100, 2))

print('\nLOG2 Function - logarithmic Value of 120 Base 2 = ', math.log2(120))

print('\nLOG10 Function - logarithmic Value of 150 Base 10 = ', math.log2(150))

print('\nPOW Function - 2 Power 3 Value = ', math.pow(2, 3))
print('POW Function - 5 Power 4 Value = ', math.pow(5, 4))

print('\nSQRT Function - Square Root of 25 = ', math.sqrt(25))
print('SQRT Function - Square Root of 19 = ', math.sqrt(19))

Python Logarithmic Functions output

EXP Function - exp Value of 5 =  148.4131591025766
EXP Function - exp Value of -3 =  0.049787068367863944

EXPM1 Function - expm1 Value of 8 =  2979.9579870417283
EXPM1 Function - expm1 Value of -5 =  -0.9932620530009145

LOG Function - logarithmic Value of 5 =  1.6094379124341003
LOG Function - logarithmic Value of 100 Base 2 =  6.643856189774725

LOG2 Function - logarithmic Value of 120 Base 2 =  6.906890595608519

LOG10 Function - logarithmic Value of 150 Base 10 =  7.22881869049588

POW Function - 2 Power 3 Value =  8.0
POW Function - 5 Power 4 Value =  625.0

SQRT Function - Square Root of 25 =  5.0
SQRT Function - Square Root of 19 =  4.358898943540674

Python Trigonometric Functions Example 1

In this Python Trigonometric Function example, we are going to use the sin, cos, and tan functions to find the Sine, Cosine, and Tangent Values. Next, we used the acos, asin, atan, and atan2 functions to find the Arc cosine, Arc Sine, and Arc Tangent values. Within the last statement, we used the hypot function

# Python Trigonometric Functions Examples
import math

print('COS Function - Cosine of 10 = ', math.cos(10))
print('COS Function - Cosine of -15 = ', math.cos(-15))

print('\nSIN Function - Sine of 3 = ', math.sin(3))
print('SIN Function - Sine of -5 = ', math.sin(-5))

print('\nTAN Function - Tangent of 9 = ', math.tan(9))
print('TAN Function - Tangent of -3 = ', math.tan(-3))

print('\nACOS Function - Arc Cosine of 1 = ', math.acos(1))
print('ACOS Function - Arc Cosine of -0.78 = ', math.acos(-0.78))

print('\nASIN Function - Arc Sine of 1 = ', math.asin(1))
print('ASIN Function - Arc Sine of -2 = ', math.asin(-0.42))

print('\nATAN Function - Arc Tangent of 0.72 = ', math.atan(0.72))
print('ATAN Function - Arc Tangent of -2.71 = ', math.atan(-2.71))

print('\nATAN2 Function - Tangent of 2, 5 = ', math.atan2(2, 5))

print('\nHYPOT Function - Hypot Value of 2, 3 = ', math.hypot(2, 3))

Python Trigonometric Functions output

COS Function - Cosine of 10 =  -0.8390715290764524
COS Function - Cosine of -15 =  -0.7596879128588212

SIN Function - Sine of 3 =  0.1411200080598672
SIN Function - Sine of -5 =  0.9589242746631385

TAN Function - Tangent of 9 =  -0.4523156594418099
TAN Function - Tangent of -3 =  0.1425465430742778

ACOS Function - Arc Cosine of 1 =  0.0
ACOS Function - Arc Cosine of -0.78 =  2.4654621440291318

ASIN Function - Arc Sine of 1 =  1.5707963267948966
ASIN Function - Arc Sine of -2 =  -0.43344532006988595

ATAN Function - Arc Tangent of 0.72 =  0.6240230529767569
ATAN Function - Arc Tangent of -2.71 =  -1.2172930308235297

ATAN2 Function - Tangent of 2, 5 =  0.3805063771123649

HYPOT Function - Hypot Value of 2, 3 =  3.6055512754639896

Python Trigonometric Functions Example 2

In this Python Trigonometric Function example, we use the Hyperbolic functions. First, we used the cosh, sinh, and tanh functions to find the Hyperbolic Cosine, Sine, and Tangent Values. Next, acosh, asinh, and atanh functions to find the Hyperbolic Arc cosine, Arc Sine, and Hyperbolic Arc Tangent value.

# Python Trigonometric Functions Examples

import math

print('COSH Function - Hyperbolic Cosine of 2 = ', math.cosh(2))
print('COSH Function - Hyperbolic Cosine of -1 = ', math.cosh(-1))

print('\nSINH Function - Hyperbolic Sine of 3 = ', math.sinh(3))
print('SINH Function - Hyperbolic Sine of -5 = ', math.sinh(-5))

print('\nTANH Function - Hyperbolic Tangent of 1 = ', math.tanh(1))
print('TANH Function - Hyperbolic Tangent of -3 = ', math.tanh(-3))

print('\nACOSH Function - Hyperbolic Arc Cosine of 10 = ', math.acosh(10))
print('ACOSH Function - Hyperbolic Arc Cosine of 30.78 = ', math.acosh(30.78))

print('\nASINH Function - Hyperbolic Arc Sine of 15 = ', math.asinh(15))
print('ASINH Function - Hyperbolic Arc Sine of -25 = ', math.asinh(-25))

print('\nATANH Function - Hyperbolic Arc Tangent of 0.57 = ', math.atanh(0.57))
print('ATANH Function - Hyperbolic Arc Tangent of -0.71 = ', math.atanh(-0.71))

Python Hyperbolic Trigonometric Functions output

COSH Function - Hyperbolic Cosine of 2 =  3.7621956910836314
COSH Function - Hyperbolic Cosine of -1 =  1.5430806348152437

SINH Function - Hyperbolic Sine of 3 =  10.017874927409903
SINH Function - Hyperbolic Sine of -5 =  -74.20321057778875

TANH Function - Hyperbolic Tangent of 1 =  0.7615941559557649
TANH Function - Hyperbolic Tangent of -3 =  -0.9950547536867305

ACOSH Function - Hyperbolic Arc Cosine of 10 =  2.993222846126381
ACOSH Function - Hyperbolic Arc Cosine of 30.78 =  4.119748326708938

ASINH Function - Hyperbolic Arc Sine of 15 =  3.4023066454805946
ASINH Function - Hyperbolic Arc Sine of -25 =  -3.9124227656412556

ATANH Function - Hyperbolic Arc Tangent of 0.57 =  0.6475228448273728
ATANH Function - Hyperbolic Arc Tangent of -0.71 =  -0.8871838632580928

Python Angular and Special Functions Example

In this Python Angular Functions example, we used the degrees and radians functions to convert degrees to radians vice versa. Next, we used the gamma and lgamma functions to return the gamma values.

# Python Angular and Special Functions Examples

import math

print('DEGREES Function - Degrees Value of 6 = ', math.degrees(5))
print('DEGREES Function - Degrees Value of 12 = ', math.degrees(12))

print('\nRADIANS Function - Radians Value of 350 = ', math.radians(350))
print('\nRADIANS Function - Radians Value of 680 = ', math.radians(680))

print('\nGAMMA Function - Gamma Value of 8 = ', math.gamma(8))
print('LGAMMA Function - LGamma Value of 9 = ', math.lgamma(9))
Python Math Functions 8