Python Math Module

About Python Math Module

The math module in Python provides a wide range of mathematical functions and constants that you can use to perform various mathematical operations. It is part of the Python Standard Library, so you don't need to install any additional packages to use it.

The math module includes functions for basic arithmetic operations, trigonometric functions, logarithmic functions, and more. It also provides constants such as pi and e that are commonly used in mathematical calculations.

Constants Variables in Python Math Module

Here are some commonly used constants in the math module:

Function Description Example
math.pi Returns the value of π (pi), approximately 3.14159. math.pi returns 3.141592653589793
math.e Returns the value of e (Euler's number), approximately 2.71828. math.e returns 2.718281828459045
math.tau Returns the value of τ (tau), which is 2π, approximately 6.28318. math.tau returns 6.283185307179586
math.inf Represents positive infinity. math.inf returns inf
math.nan Represents "Not a Number" (NaN). math.nan returns nan
Example of Constants Variables in Python Math Module

Trigonometric and Angular Functions in Python Math Module

Trigonometric functions are mathematical functions that relate the angles of a triangle to the lengths of its sides. In Python, the math module provides several trigonometric and angular functions that you can use to perform calculations involving angles and triangles.

Here are some commonly used trigonometric functions in the math module:

Function Description Example
math.sin(x) Returns the sine of x (x is in radians). math.sin(math.pi / 2) returns 1.0
math.cos(x) Returns the cosine of x (x is in radians). math.cos(0) returns 1.0
math.tan(x) Returns the tangent of x (x is in radians). math.tan(math.pi / 4) returns 1.0
math.asin(x) Returns the arcsine of x (result is in radians). math.asin(1) returns 1.5707963267948966 (which is π/2)
math.acos(x) Returns the arccosine of x (result is in radians). math.acos(1) returns 0.0
math.atan(x) Returns the arctangent of x (result is in radians). math.atan(1) returns 0.7853981633974483 (which is π/4)
math.hypot(x, y) Returns the Euclidean norm, sqrt(x*x + y*y). math.hypot(3, 4) returns 5.0
math.degrees(x) Converts angle x from radians to degrees. math.degrees(math.pi) returns 180.0
math.radians(x) Converts angle x from degrees to radians. math.radians(180) returns 3.141592653589793
math.cosh(x) Returns the hyperbolic cosine of x. math.cosh(0) returns 1.0
math.sinh(x) Returns the hyperbolic sine of x. math.sinh(0) returns 0.0
math.tanh(x) Returns the hyperbolic tangent of x. math.tanh(0) returns 0.0
math.acosh(x) Returns the inverse hyperbolic cosine of x. math.acosh(1) returns 0.0
math.asinh(x) Returns the inverse hyperbolic sine of x. math.asinh(0) returns 0.0
math.atanh(x) Returns the inverse hyperbolic tangent of x. math.atanh(0) returns 0.0
math.isclose(a, b, rel_tol=1e-09, abs_tol=0.0) Determines whether two floating-point numbers are close in value. math.isclose(0.1 + 0.2, 0.3) returns True
math.dist(p, q) Returns the Euclidean distance between two points p and q. math.dist((1, 2), (4, 6)) returns 5.0
math.prod(iterable, *, start=1) Returns the product of a start value (default: 1) times an iterable of numbers. math.prod([1, 2, 3, 4]) returns 24
Example of Trigonometric Functions in Python Math Module

Power, Exponents & Logarithms Functions in Python Math Module

The math module in Python provides several functions for working with power, exponents and logarithms. These functions allow you to perform calculations involving powers, roots, and logarithmic values.

Here are some commonly used exponent and logarithm functions in the math module:

Function Description Example
math.pow(x, y) Returns x raised to the power of y (x^y). math.pow(2, 3) returns 8.0
math.sqrt(x) Returns the square root of x. math.sqrt(16) returns 4.0
math.cbrt(x) Returns the cube root of x. math.cbrt(27) returns 3.0
math.exp(x) Returns e raised to the power of x (e^x). math.exp(1) returns 2.718281828459045
math.exp2(x) Returns 2 raised to the power of x (2^x). math.exp2(3) returns 8.0
math.expm1(x) Returns e raised to the power of x minus 1 (e^x - 1). math.expm1(1) returns 1.718281828459045
math.log(x, base) Returns the logarithm of x to the specified base. If the base is not provided, it defaults to e (natural logarithm). math.log(100, 10) returns 2.0
math.log10(x) Returns the base-10 logarithm of x. math.log10(100) returns 2.0
math.log2(x) Returns the base-2 logarithm of x. math.log2(8) returns 3.0
Example of Power, Exponents & Logarithms Functions in Python Math Module

Number Theory Functions in Python Math Module

The math module in Python provides several functions for working with number theory. These functions allow you to perform calculations involving integers, prime numbers, and other number-theoretic concepts.

Here are some commonly used number theory functions in the math module:

Function Description Example
math.gcd(a, b) Returns the greatest common divisor of a and b. math.gcd(48, 18) returns 6
math.lcm(a, b) Returns the least common multiple of a and b. math.lcm(4, 5) returns 20
math.isqrt(n) Returns the integer square root of n. math.isqrt(16) returns 4
math.factorial(n) Returns the factorial of n. math.factorial(5) returns 120
math.comb(n, k) Returns the number of ways to choose k items from n items without repetition and without order (combinations). math.comb(5, 2) returns 10
math.perm(n, k) Returns the number of ways to choose k items from n items without repetition and with order (permutations). math.perm(5, 2) returns 20
Example of Number Theory Functions in Python Math Module

Rounding & Modulus Functions in Python Math Module

The math module in Python provides several functions for rounding numbers and performing modulus operations. These functions allow you to round numbers to the nearest integer, round up or down, and calculate the remainder of a division operation.

Here are some commonly used rounding and modulus functions in the math module:

Function Description Example
math.ceil(x) Returns the smallest integer greater than or equal to x (rounds up). math.ceil(4.2) returns 5
math.floor(x) Returns the largest integer less than or equal to x (rounds down). math.floor(4.7) returns 4
math.trunc(x) Returns the integer part of x, removing the decimal part (truncates). math.trunc(4.7) returns 4
math.fabs(x) Returns the absolute value of x float number. math.fabs(4.017) returns 4.017
math.fmod(x, y) Returns the remainder of x divided by y (floating-point modulus). math.fmod(5.3, 2) returns 1.2999999999999998
math.remainder(x, y) Returns the IEEE 754-style remainder of x with respect to y. math.remainder(5.3, 2) returns -0.7000000000000002
math.modf(x) Returns the fractional and integer parts of x as a tuple (fractional part, integer part). math.modf(4.7) returns (0.6999999999999993, 4.0)
Example of Rounding & Modulus Functions in Python Math Module

Floating-Point Utilities Functions in Python Math Module

The math module in Python provides several floating-point utilities functions for working with floating-point number. These functions allow you to check for special floating-point values, and perform other operations related to floating-point arithmatic.

Here are some commonly used floating-point utilities functions in the math module:

Function Description Example
math.copysign(x, y) Returns a float with the magnitude of x and the sign of y. math.copysign(3.0, -1.0) returns -3.0
math.frexp(x) Returns the mantissa and exponent of x as the pair (m, e). math.frexp(8.0) returns (0.5, 4)
math.ldexp(m, e) Returns m * (2**e). math.ldexp(0.5, 4) returns 8.0
math.fsum(iterable) Returns an accurate floating-point sum of values in the iterable. math.fsum([0.1, 0.2, 0.3]) returns 0.6000000000000001
math.prod(iterable) Returns the product of a start value (default: 1) times an iterable of numbers. math.prod([1, 2, 3, 4]) returns 24
math.dist(p, q) Returns the Euclidean distance between two points p and q. math.dist((1, 2), (4, 6)) returns 5.0
math.hypot(x, y, …) Returns the Euclidean norm, sqrt(x*x + y*y + ...). math.hypot(3, 4) returns 5.0
math.isclose(a, b, rel_tol=1e-09, abs_tol=0.0) Determines whether two floating-point numbers are close in value. math.isclose(0.1 + 0.2, 0.3) returns True
math.isfinite(x) Returns True if x is neither infinite nor NaN (Not a Number). math.isfinite(4.5) returns True
math.isinf(x) Returns True if x is positive or negative infinity. math.isinf(float('inf')) returns True
math.isnan(x) Returns True if x is NaN (Not a Number). math.isnan(float('nan')) returns True
math.ulp(x) Returns the value of the least significant bit of the floating-point number x. math.ulp(1.0) returns 2.220446049250313e-16
math.nextafter(x, y) Returns the next representable floating-point value after x in the direction of y. math.nextafter(1.0, 2.0) returns 1.0000000000000002
Example of Floating-Point Utilities Functions in Python Math Module

Special Functions in Python Math Module

The math module in Python provides several special functions for statistics, probability, and scientific computing.

Here are some commonly used special functions in the math module:

Function Description Example
math.gamma(x) Returns the gamma function at x. math.gamma(5) returns 24.0
math.lgamma(x) Returns the natural logarithm of the absolute value of the gamma function at x. math.lgamma(5) returns 3.1780538303479453
math.erf(x) Returns the error function at x. math.erf(1) returns 0.8427007929497149
math.erfc(x) Returns the complementary error function at x. math.erfc(1) returns 0.1572992070502851
Example of Special Functions in Python Math Module

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Apply Math module functions with different values in the following code editor and click Execute button. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output