App: Math Module#
The math module provides most commonly used mathematical functions and constants.
First, you need to import the math module.
import math
You can access the list of constants and functions in the math module by using the dir(math) function.
You can execute help(math) for more details.
print(dir(math))
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'cbrt', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'exp2', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
Constants#
pi#
\(\pi\) (pi): math.pi
print(math.pi)
3.141592653589793
e#
\(e\) (Euler’s constant): math.e
print(math.e)
2.718281828459045
Functions#
radians()#
It is the conversion function from degress to radians. The syntax is math.radians()
# 180 degrees = 1 pi radians
print(math.radians(180))
3.141592653589793
# 270 degrees = 3 pi /2 radians
print(math.radians(270))
4.71238898038469
degrees()#
It is the conversion function from radians to degrees. The syntax is math.degrees()
# 180 degrees = 1 pi radians
print(math.degrees(math.pi))
180.0
# 270 degrees = 3 pi /2 radians
print(math.degrees(3*math.pi/2))
270.0
cos()#
It is the cosine function. The syntax is math.cos()
The input angle is in radians.
# cos(0)
print(math.cos(0))
1.0
# cos(pi/2), actual value is 0
print(math.cos(math.pi/2))
6.123233995736766e-17
# cos(pi)
print(math.cos(math.pi))
-1.0
# cos(3pi/2), actual value is 0
print(math.cos(3*math.pi/2))
-1.8369701987210297e-16
# cos(2pi)
print(math.cos(2*math.pi))
1.0
sin()#
It is the sine function. The syntax is math.sin()
The input angle is in radians.
# sin(0)
print(math.sin(0))
0.0
# sin(pi/2)
print(math.sin(math.pi/2))
1.0
# sin(pi), actual value is 0
print(math.sin(math.pi))
1.2246467991473532e-16
# sin(3pi/2)
print(math.sin(3*math.pi/2))
-1.0
# cos(2pi), actual value is 0
print(math.sin(2*math.pi))
-2.4492935982947064e-16
tan()#
It is the tangent function. The syntax is math.tan()
The input angle is in radians.
# tan(0)
print(math.tan(0))
0.0
# tan(pi), actual value is 0
print(math.tan(math.pi))
-1.2246467991473532e-16
acos()#
It is the arccosine function which is the inverse of the cosine function. The syntax is math.acos()
# acos(1) = 0 because cos(0) = 1
print(math.acos(1))
0.0
asin()#
It is the arcsine function which is the inverse of the sine function. The syntax is math.asin()
# asin(1) = pi/2 because sin(pi/2) = 1
print(math.asin(1))
1.5707963267948966
atan()#
It is the arctangent function which is the inverse of the tangent function. The syntax is math.atan()
# atan(0) = 0 because tan(0) = 0
print(math.atan(0))
0.0
cosh()#
It is the hyperbolic cosine function. The syntax is math.cosh()
\(\displaystyle cosh(x) = \frac{e^x+e^{-x}}{2}\)
print(math.cosh(0))
1.0
print(math.cosh(1))
1.5430806348152437
sinh()#
It is the hyperbolic sine function. The syntax is math.sinh()
\(\displaystyle sinh(x) = \frac{e^x-e^{-x}}{2}\)
print(math.sinh(0))
0.0
print(math.sinh(1))
1.1752011936438014
tanh()#
It is the hyperbolic tangent function. The syntax is math.tanh()
\(\displaystyle tanh(x) = \frac{e^x-e^{-x}}{e^x+e^{-x}}\)
print(math.tanh(0))
0.0
print(math.tanh(1))
0.7615941559557649
lcm()#
It is the least common multiple function. The syntax is math.lcm()
print(math.lcm(12, 20))
60
gcd()#
It is the greatest common divisor function. The syntax is math.gcd()
print(math.gcd(12,20))
4
log()#
It is the logarithm to the base of \(e\) function \(\log_e(x)=ln(x)\). The syntax is math.log()
print(math.log(math.e))
1.0
log10()#
It is the logarithm to the base of \(e\) function \(\log_{10}(x)=log(x)\). The syntax is math.log10()
print(math.log10(1000))
3.0
factorial()#
It is the factorial function. The syntax is math.factorial()
5 factorial is denoted by \(5!\) in mathematics and it is equal to \(5! = 5\cdot4\cdot3\cdot2\cdot1\)
print(math.factorial(5))
120
sqrt()#
It is the square root function. The syntax is math.sqrt()
print(math.sqrt(36))
6.0
comb()#
It returns the number of combinations of choosing k items from n items without repetition and without considering the order.
It’s also equivalent to the number of subsets with k elements in a set containing n elements.
Example: for the set {a,b,c,d,e} the subsets with 2 elements are:
{a,b}, {a,c}, {a,d}, {a,e}, {b,c}, {b,d}, {b,e}, {c,d}, {c,e}, {d,e}
comb(n,k) is denoted by \(C(n,k)\) in mathematics and it is equal to \(\displaystyle C(n,k) = \frac{n!}{(n-k)!\cdot k!}\)
The syntax is math.comb(n, k)
print(math.comb(5,2))
10
perm()#
It returns the number of permutations of choosing k items from n items without repetition but considering the order.
Example: for the set {a,b,c,d,e} the permutations with 2 elements are:
ab, ac, ad, ae, bc, bd, be, cd, ce, de
ba, ca, da, ea, cb, db, eb, dc, ec, ed
perm(n,k) is denoted by \(P(n,k)\) in mathematics and it is equal to \(\displaystyle P(n,k) = \frac{n!}{(n-k)!}\)
The syntax is math.perm(n, k)
print(math.perm(5,2))
20
ceil()#
It is the the smallest integer greater than or equal to the input number. The syntax is math.ceil()
print(math.ceil(4.567))
5
print(math.ceil(9.2))
10
floor()#
It is the the largest integer less than or equal to the input number. The syntax is math.floor()
print(math.floor(4.567))
4
print(math.floor(9.2))
9