The math module is a built-in module in Python that is used for performing mathematical operations. This module provides various built-in methods for performing different mathematical tasks.
Note: The math module's methods do not work with complex numbers. For that, you can use the cmath module.
Importing math ModuleBefore using the methods of the math module, you need to import the math module into your code. The following is the syntax:
import mathMethods of Python math Module
The following is the list of math module methods that we have categorized based on their functionality and usage.
Math Module - Theoretic and Representation MethodsPython includes following theoretic and representation Functions in the math module −
Sr.No. Function & Description 1The ceiling of x: the smallest integer not less than x
2 math.comb(n,k)This function is used to find the returns the number of ways to choose "x" items from "y" items without repetition and without order.
3This function returns a float with the magnitude (absolute value) of x but the sign of y.
4This function is used to compare the values of to objects. This function is deprecated in Python3.
5This function is used to calculate the absolute value of a given integer.
6This function is used to find the factorial of a given integer.
7This function calculates the floor value of a given integer.
8The fmod() function in math module returns same result as the "%" operator. However fmod() gives more accurate result of modulo division than modulo operator.
9This function is used to calculate the mantissa and exponent of a given number.
10This function returns the floating point sum of all numeric items in an iterable i.e. list, tuple, array.
11This function is used to calculate the greatest common divisor of all the given integers.
12This function is used to determine whether two given numeric values are close to each other.
13This function is used to determine whether the given number is a finite number.
14This function is used to determine whether the given value is infinity (+ve or, -ve).
15This function is used to determine whether the given number is "NaN".
16This function calculates the integer square-root of the given non negative integer.
17This function is used to calculate the least common factor of the given integer arguments.
18This function returns product of first number with exponent of second number. So, ldexp(x,y) returns x*2**y. This is inverse of frexp() function.
19This returns the fractional and integer parts of x in a two-item tuple.
20This function returns the next floating-point value after x towards y.
21This function is used to calculate the permutation. It returns the number of ways to choose x items from y items without repetition and with order.
22This function is used to calculate the product of all numeric items in the iterable (list, tuple) given as argument.
23This function returns the remainder of x with respect to y. This is the difference x − n*y, where n is the integer closest to the quotient x / y.
24This function returns integral part of the number, removing the fractional part. trunc() is equivalent to floor() for positive x, and equivalent to ceil() for negative x.
25This function returns the value of the least significant bit of the float x. trunc() is equivalent to floor() for positive x, and equivalent to ceil() for negative x.
Math Module - Power and Logarithmic Methods Sr.No. Function & Description 1This function is used to calculate the cube root of a number.
2This function calculate the exponential of x: ex
3This function returns 2 raised to power x. It is equivalent to 2**x.
4This function returns e raised to the power x, minus 1. Here e is the base of natural logarithms.
5This function calculates the natural logarithm of x, for x> 0.
6This function returns the natural logarithm of 1+x (base e). The result is calculated in a way which is accurate for x near zero.
7This function returns the base-2 logarithm of x. This is usually more accurate than log(x, 2).
8The base-10 logarithm of x for x> 0.
9The value of x**y.
10The square root of x for x > 0
Math Module - Trigonometric MethodsPython includes following functions that perform trigonometric calculations in the math module −
Sr.No. Function & Description 1This function returns the arc cosine of x, in radians.
2This function returns the arc sine of x, in radians.
3This function returns the arc tangent of x, in radians.
4This function returns atan(y / x), in radians.
5This function returns the cosine of x radians.
6This function returns the sine of x radians.
7This function returns the tangent of x radians.
8This function returns the Euclidean norm, sqrt(x*x + y*y).
Math Module - Angular conversion MethodsFollowing are the angular conversion function provided by Python math module −
Sr.No. Function & Description 1This function converts the given angle from radians to degrees.
2This function converts the given angle from degrees to radians.
Math Module - Mathematical ConstantsThe Python math module defines the following mathematical constants −
Sr.No. Constants & Description 1This represents the mathematical constant pi, which equals to "3.141592..." to available precision.
2This represents the mathematical constant e, which is equal to "2.718281..." to available precision.
3This represents the mathematical constant Tau (denoted by ). It is equivalent to the ratio of circumference to radius, and is equal to 2.
4This represents positive infinity. For negative infinity use "−math.inf".
5This constant is a floating-point "not a number" (NaN) value. Its value is equivalent to the output of float('nan').
Math Module - Hyperbolic MethodsHyperbolic functions are analogs of trigonometric functions that are based on hyperbolas instead of circles. Following are the hyperbolic functions of the Python math module −
Sr.No. Function & Description 1This function is used to calculate the inverse hyperbolic cosine of the given value.
2This function is used to calculate the inverse hyperbolic sine of a given number.
3This function is used to calculate the inverse hyperbolic tangent of a number.
4This function is used to calculate the hyperbolic cosine of the given value.
5This function is used to calculate the hyperbolic sine of a given number.
6This function is used to calculate the hyperbolic tangent of a number.
Math Module - Special MethodsFollowing are the special functions provided by the Python math module −
Sr.No. Function & Description1
This function returns the value of the Gauss error function for the given parameter.
2
This function is the complementary for the error function. Value of erf(x) is equivalent to 1-erf(x).
3
This is used to calculate the factorial of the complex numbers. It is defined for all the complex numbers except the non-positive integers.
4
This function is used to calculate the natural logarithm of the absolute value of the Gamma function at x.
Example UsageThe following example demonstrates the use of math module and its methods:
# Importing math Module import math # Using methods of math module print(math.sqrt(9)) print(math.pow(3, 3)) print(math.exp(1)) print(math.log(100, 10)) print(math.factorial(4)) print(math.gcd(12, 3))
Output
3.0 27.0 2.718281828459045 2.0 24 3
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4