Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Python Math Functions

by suresh

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 Functions available in Python Math Library

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 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 Functions 1

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 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 Mathematical Functions 2

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 Mathematical Functions 3

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 Math Functions 4

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 Math Functions 5

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 Math Functions 6

Python Trigonometric Functions Example 2

In this Python Math 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 Math Functions 7

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

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy