18 MathModuleFunctionsinPython-JupyterNotebook
18 MathModuleFunctionsinPython-JupyterNotebook
In [1]:
In [2]:
1 # Many functions regarding math modules in python can be find using helf(math) method.
2 help(math)
NAME
math
DESCRIPTION
This module provides access to the mathematical functions
defined by the C standard.
FUNCTIONS
acos(x, /)
Return the arc cosine (measured in radians) of x.
acosh(x, /)
Return the inverse hyperbolic cosine of x.
asin(x, /)
Return the arc sine (measured in radians) of x
acos() function
Return the arc cosine (measured in radians) of x.
The result is between 0 and pi.
The parameter must be a double value between -1 and 1.
In [54]:
1 nlis = []
2 nlis.append(math.acos(1))
3 nlis.append(math.acos(-1))
4 print(nlis)
[0.0, 3.141592653589793]
acosh() function
It is a built-in method defined under the math module to calculate the hyperbolic arc cosine of the given
parameter in radians.
For example, if x is passed as an acosh function (acosh(x)) parameter, it returns the hyperbolic arc cosine
value.
In [56]:
1 print(math.acosh(1729))
8.148445582615551
asin() function
Return the arc sine (measured in radians) of x.
The result is between -pi/2 and pi/2.
In [52]:
1 nlis = []
2 nlis.append(math.asin(1))
3 nlis.append(math.asin(-1))
4 print(nlis)
5
[1.5707963267948966, -1.5707963267948966]
asinh() function
Return the inverse hyperbolic sine of x.
In [55]:
1 print(math.asinh(1729))
8.1484457498709
atan() function
Return the arc tangent (measured in radians) of x.
The result is between -pi/2 and pi/2.
localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Modul… 2/21
15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook
e esu t s bet ee p/ a dp/
In [66]:
1 nlis = []
2 nlis.append(math.atan(math.inf)) # pozitive infinite
3 nlis.append(math.atan(-math.inf)) # negative infinite
4 print(nlis)
[1.5707963267948966, -1.5707963267948966]
atan2() function
Return the arc tangent (measured in radians) of y/x.
Unlike atan(y/x), the signs of both x and y are considered.
In [74]:
1 print(math.atan2(1729, 37))
2 print(math.atan2(1729, -37))
3 print(math.atan2(-1729, -37))
4 print(math.atan2(-1729, 37))
5 print(math.atan2(math.pi, math.inf))
6 print(math.atan2(math.inf, math.e))
7 print(math.atan2(math.tau, math.pi))
1.5493999395414435
1.5921927140483498
-1.5921927140483498
-1.5493999395414435
0.0
1.5707963267948966
1.1071487177940904
atanh() function
Return the inverse hyperbolic tangent of x.
In [91]:
1 nlis=[]
2 nlis.append(math.atanh(-0.9999))
3 nlis.append(math.atanh(0))
4 nlis.append(math.atanh(0.9999))
5 print(nlis)
6
ceil() function
Rounds a number up to the nearest integer
Returns the smalles integer greater than or equal to variable.
In [22]:
comb() function
Number of ways to choose k items from n items without repetition and without order.
Evaluates to n!/(k!*(n - k)!) when k <= n and evaluates to zero when k>n.
Also called the binomial coefficient because it is equivalent to the coefficient of k-th term in polynomial
expansion of the expression (1 + x)**n.
Raises TypeError if either of the arguments are not integers.
Raises ValueError if either of the arguments are negative.
In [19]:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9084/1573976203.py in <module>
1 print(f'The combination of 6 with 2 is {math.comb(6, 2)}.')
----> 2 print(math.comb(10, 3.14)) # It returns a TypeError
copysign() function
Returns a float consisting of the value of the first parameter and the sign of the second parameter.
In [18]:
1 print(f'The copysign of thes two numbers -3.14 and 2.718 is {math.copysign(-3.14, 2.718)}.')
2 print(f'The copysign of thes two numbers 1729 and -0.577 is {math.copysign(1729, -0.577)}.')
cos() function
Return the cosine of x (measured in radians).
In [105]:
1 print(math.cos(0))
2 print(math.cos(math.pi/6))
3 print(math.cos(-1))
4 print(math.cos(1))
5 print(math.cos(1729))
6 print(math.cos(90))
1.0
0.8660254037844387
0.5403023058681398
0.5403023058681398
0.43204202084333315
-0.4480736161291701
cosh() function
Return the hyperbolic cosine of x.
In [114]:
1 nlis = []
2 nlis.append(math.cosh(1))
3 nlis.append(math.cosh(0))
4 nlis.append(math.cosh(-5))
5 print(nlis)
degrees() function
Convert angle x from radians to degrees.
In [122]:
1 nlis = []
2 nlis.append(math.degrees(math.pi/2))
3 nlis.append(math.degrees(math.pi))
4 nlis.append(math.degrees(math.pi/4))
5 nlis.append(math.degrees(-math.pi))
6 print(nlis)
dist() function
Return the Euclidean distance between two points p and q.
The points should be specified as sequences (or iterables) of coordinates.
Both inputs must have the same dimension.
Roughly equivalent to: sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))
In [127]:
1 print(math.dist([30], [60]))
2 print(math.dist([0.577, 1.618], [3.14, 2.718]))
3 x = [0.577, 1.618, 2.718]
4 y = [6, 28, 37]
5 print(math.dist(x, y))
30.0
2.7890803143688783
43.59672438383416
erf() function
Error function at x.
This method accepts a value between - inf and + inf, and returns a value between - 1 to + 1.
In [136]:
1 nlis = []
2 nlis.append(math.erf(math.inf))
3 nlis.append(math.erf(math.pi))
4 nlis.append(math.erf(math.e))
5 nlis.append(math.erf(math.tau))
6 nlis.append(math.erf(0))
7 nlis.append(math.erf(6))
8 nlis.append(math.erf(1.618))
9 nlis.append(math.erf(0.577))
10 nlis.append(math.erf(-math.inf))
11 print(nlis)
erfc() function
Complementary error function at x.
This method accepts a value between - inf and + inf, and returns a value between 0 and 2.
In [137]:
1 nlis = []
2 nlis.append(math.erfc(math.inf))
3 nlis.append(math.erfc(math.pi))
4 nlis.append(math.erfc(math.e))
5 nlis.append(math.erfc(math.tau))
6 nlis.append(math.erfc(0))
7 nlis.append(math.erfc(6))
8 nlis.append(math.erfc(1.618))
9 nlis.append(math.erfc(0.577))
10 nlis.append(math.erfc(-math.inf))
11 print(nlis)
exp() function
The math.exp() method returns E raised to the power of x (Ex).
E is the base of the natural system of logarithms (approximately 2.718282) and x is the number passed to
it.
In [139]:
1 nlis = []
2 nlis.append(math.exp(math.inf))
3 nlis.append(math.exp(math.pi))
4 nlis.append(math.exp(math.e))
5 nlis.append(math.exp(math.tau))
6 nlis.append(math.exp(0))
7 nlis.append(math.exp(6))
8 nlis.append(math.exp(1.618))
9 nlis.append(math.exp(0.577))
10 nlis.append(math.exp(-math.inf))
11 print(nlis)
expm1() function
Return exp(x)-1.
This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.
In [141]:
1 nlis = []
2 nlis.append(math.expm1(math.inf))
3 nlis.append(math.expm1(math.pi))
4 nlis.append(math.expm1(math.e))
5 nlis.append(math.expm1(math.tau))
6 nlis.append(math.expm1(0))
7 nlis.append(math.expm1(6))
8 nlis.append(math.expm1(1.618))
9 nlis.append(math.expm1(0.577))
10 nlis.append(math.expm1(-math.inf))
11 print(nlis)
fabs() function
Returns the absolute value of a number
In [14]:
factorial() function
Returns the factorial of a number.
In [28]:
In [29]:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9084/3052214312.py in <module>
1 # Factorial of negative numbers returns a ValueError.
----> 2 print(math.factorial(-6))
In [30]:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9084/3264451390.py in <module>
1 # Factorial of non-unteger numbers returns a TypeError.
----> 2 print(math.factorial(3.14))
floor() functions:
Rounds a number down to the nearest integer
In [34]:
1 print(math.floor(3.14))
fmod() function
Returns the remainder of x/y
In [37]:
1 print(math.fmod(37, 6))
2 print(math.fmod(1728, 37))
1.0
26.0
frexp() function
Returns the mantissa and the exponent, of a specified number
In [31]:
1 print(math.frexp(2.718))
(0.6795, 2)
fsum() function
Returns the sum of all items in any iterable (tuples, arrays, lists, etc.)
In [142]:
1808.053
gamma() function
Returns the gamma function at x.
You can find more information about gamma function from this Link.
(https://en.wikipedia.org/wiki/Gamma_function)
In [143]:
1 print(math.gamma(3.14))
2 print(math.gamma(6))
3 print(math.gamma(2.718))
2.2844806338178008
120.0
1.5671127417668826
gcd() function
Returns the greatest common divisor of two integers
In [144]:
1 print(math.gcd(3, 10))
2 print(math.gcd(4, 8))
3 print(math.gcd(0, 0))
1
4
0
hypot() function
Returns the Euclidean norm.
Multidimensional Euclidean distance from the origin to a point.
Roughly equivalent to: sqrt(sum(x**2 for x in coordinates))
For a two dimensional point (x, y), gives the hypotenuse using the Pythagorean theorem: sqrt(xx + yy).
In [148]:
1 print(math.hypot(3, 4))
2 print(math.hypot(5, 12))
3 print(math.hypot(8, 15))
5.0
13.0
17.0
isclose() function
It checks whether two values are close to each other, or not.
Returns True if the values are close, otherwise False.
This method uses a relative or absolute tolerance, to see if the values are close.
Tip: It uses the following formula to compare the values: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)),
abs_tol)
In [11]:
False
False
False
True
True
isfinite() function
Return True if x is neither an infinity nor a NaN, and False otherwise.
In [155]:
1 nlis = []
2 nlis.append(math.isfinite(math.inf))
3 nlis.append(math.isfinite(math.pi))
4 nlis.append(math.isfinite(math.e))
5 nlis.append(math.isfinite(math.tau))
6 nlis.append(math.isfinite(0))
7 nlis.append(math.isfinite(6))
8 nlis.append(math.isfinite(1.618))
9 nlis.append(math.isfinite(0.577))
10 nlis.append(math.isfinite(-math.inf))
11 nlis.append(math.isfinite(float('NaN')))
12 nlis.append(math.isfinite(float('inf')))
13 print(nlis)
[False, True, True, True, True, True, True, True, False, False, False]
isinf() function
Return True if x is a positive or negative infinity, and False otherwise.
In [161]:
1 nlis = []
2 nlis.append(math.isinf(math.inf))
3 nlis.append(math.isinf(math.pi))
4 nlis.append(math.isinf(math.e))
5 nlis.append(math.isinf(math.tau))
6 nlis.append(math.isinf(0))
7 nlis.append(math.isinf(6))
8 nlis.append(math.isinf(1.618))
9 nlis.append(math.isinf(0.577))
10 nlis.append(math.isinf(-math.inf))
11 print(nlis)
isnan() function
Return True if x is a NaN (not a number), and False otherwise.
In [162]:
1 nlis = []
2 nlis.append(math.isnan(float('NaN')))
3 nlis.append(math.isnan(math.inf))
4 nlis.append(math.isnan(math.pi))
5 nlis.append(math.isnan(math.e))
6 nlis.append(math.isnan(math.tau))
7 nlis.append(math.isnan(0))
8 nlis.append(math.isnan(6))
9 nlis.append(math.isnan(1.618))
10 nlis.append(math.isnan(0.577))
11 nlis.append(math.isnan(-math.inf))
12 nlis.append(math.isnan(math.nan))
13 print(nlis)
[True, False, False, False, False, False, False, False, False, False, True]
isqrt() function
Rounds a square root number downwards to the nearest integer.
The returned square root value is the floor value of square root of a non-negative integer number.
It gives a ValueError and TypeError when a negative integer number and a float number are used,
respectively.
In [15]:
1 print(math.isqrt(4))
2 print(math.isqrt(5))
3 print(math.isqrt(-5))
2
2
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_20068/2393595883.py in <module>
1 print(math.isqrt(4))
2 print(math.isqrt(5))
----> 3 print(math.isqrt(-5))
In [16]:
1 print(math.isqrt(3.14))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_20068/4116779010.py in <module>
----> 1 print(math.isqrt(3.14))
lcm() function
Least Common Multiple.
In [168]:
1 nlis = []
2 nlis.append(math.lcm(3, 5, 25))
3 nlis.append(math.lcm(9, 6, 27))
4 nlis.append(math.lcm(21, 27, 54))
5 print(nlis)
ldexp() function
Returns the inverse of math.frexp() which is x*(2^i) of the given numbers x and i
In [19]:
1 print(math.ldexp(20, 4))
2 print(20*(2**4))
320.0
320
lgamma() function
Returns the log gamma value of x
In [26]:
1 print(math.gamma(6))
2 print(math.lgamma(6))
3 print(math.log(120)) # print(math.gamma(6)) = 120
120.0
4.787491742782047
4.787491742782046
log() function
log(x, [base=math.e])
Return the logarithm of x to the given base.
In [174]:
1 nlis = []
2 nlis.append(math.log(90))
3 nlis.append(math.log(1))
4 nlis.append(math.log(math.e))
5 nlis.append(math.log(math.pi))
6 nlis.append(math.log(math.tau))
7 nlis.append(math.log(math.inf))
8 nlis.append(math.log(math.nan))
9 print(nlis)
log10() function
Return the base 10 logarithm of x.
In [177]:
1 nlis = []
2 nlis.append(math.log10(90))
3 nlis.append(math.log10(1))
4 nlis.append(math.log10(math.e))
5 nlis.append(math.log10(math.pi))
6 nlis.append(math.log10(math.tau))
7 nlis.append(math.log10(math.inf))
8 nlis.append(math.log10(math.nan))
9 print(nlis)
log1p() function
Return the natural logarithm of 1+x (base e).
In [179]:
1 nlis = []
2 nlis.append(math.log1p(90))
3 nlis.append(math.log1p(1))
4 nlis.append(math.log1p(math.e))
5 nlis.append(math.log1p(math.pi))
6 nlis.append(math.log1p(math.tau))
7 nlis.append(math.log1p(math.inf))
8 nlis.append(math.log1p(math.nan))
9 print(nlis)
log2() function
Return the base 2 logarithm of x.
In [183]:
1 nlis = []
2 nlis.append(math.log2(90))
3 nlis.append(math.log2(2))
4 nlis.append(math.log2(1))
5 nlis.append(math.log2(math.e))
6 nlis.append(math.log2(math.pi))
7 nlis.append(math.log2(math.tau))
8 nlis.append(math.log2(math.inf))
9 nlis.append(math.log2(math.nan))
10 print(nlis)
modf() function
It returns the frwactional and integer parts of the certain number. Both the outputs carry the sign of x and
are of type float.
In [29]:
1 print(math.modf(math.pi))
2 print(math.modf(math.e))
3 print(math.modf(1.618))
(0.14159265358979312, 3.0)
(0.7182818284590451, 2.0)
(0.6180000000000001, 1.0)
nextafter() function
Return the next floating-point value after x towards y.
if x is equal to y then y is returned.
In [191]:
1 nlis = []
2 nlis.append(math.nextafter(3.14, 90))
3 nlis.append(math.nextafter(6, 2.718))
4 nlis.append(math.nextafter(3, math.e))
5 nlis.append(math.nextafter(28, math.inf))
6 nlis.append(math.nextafter(1.618, math.nan))
7 nlis.append(math.nextafter(1, 1))
8 nlis.append(math.nextafter(0, 0))
9 print(nlis)
perm() function
Returns the number of ways to choose k items from n items with order and without repetition.
In [31]:
1 print(math.perm(6, 2))
2 print(math.perm(6, 6))
30
720
pow() function
Returns the value of x to the power of y.
In [34]:
1 print(math.pow(10, 2))
2 print(math.pow(math.pi, math.e))
100.0
22.45915771836104
prod() function
Returns the product of all the elements in an iterable
In [32]:
85632659.07026622
radians() function
Convert angle x from degrees to radians.
In [193]:
1 nlis = []
2 nlis.append(math.radians(0))
3 nlis.append(math.radians(30))
4 nlis.append(math.radians(45))
5 nlis.append(math.radians(60))
6 nlis.append(math.radians(90))
7 nlis.append(math.radians(120))
8 nlis.append(math.radians(180))
9 nlis.append(math.radians(270))
10 nlis.append(math.radians(360))
11 print(nlis)
remainder() function
Difference between x and the closest integer multiple of y.
Return x - ny where ny is the closest integer multiple of y.
ReturnIn the case where x is exactly halfway between two multiples of
y, the nearest even value of n is used. The result is always exact.
In [196]:
1 nlis = []
2 nlis.append(math.remainder(3.14, 2.718))
3 nlis.append(math.remainder(6, 28))
4 nlis.append(math.remainder(5, 3))
5 nlis.append(math.remainder(1729, 37))
6 print(nlis)
sin() function
Return the sine of x (measured in radians).
Note: To find the sine of degrees, it must first be converted into radians with the math.radians() method.
In [204]:
1 nlis = []
2 nlis.append(math.sin(math.pi))
3 nlis.append(math.sin(math.pi/2))
4 nlis.append(math.sin(math.e))
5 nlis.append(math.sin(math.nan))
6 nlis.append(math.sin(math.tau))
7 nlis.append(math.sin(30))
8 nlis.append(math.sin(-5))
9 nlis.append(math.sin(37))
10 print(nlis)
sinh() function
Return the hyperbolic sine of x.
In [213]:
1 nlis = []
2 nlis.append(math.sinh(1))
3 nlis.append(math.sinh(0))
4 nlis.append(math.sinh(-5))
5 nlis.append(math.sinh(math.pi))
6 nlis.append(math.sinh(math.e))
7 nlis.append(math.sinh(math.tau))
8 nlis.append(math.sinh(math.nan))
9 nlis.append(math.sinh(math.inf))
10 print(nlis)
sqrt() function
localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Mod… 18/21
15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook
In [210]:
1 nlis = []
2 nlis.append(math.sqrt(1))
3 nlis.append(math.sqrt(0))
4 nlis.append(math.sqrt(37))
5 nlis.append(math.sqrt(math.pi))
6 nlis.append(math.sqrt(math.e))
7 nlis.append(math.sqrt(math.tau))
8 nlis.append(math.sqrt(math.nan))
9 nlis.append(math.sqrt(math.inf))
10 print(nlis)
tan() function
Return the tangent of x (measured in radians).
In [212]:
1 nlis = []
2 nlis.append(math.tan(0))
3 nlis.append(math.tan(30))
4 nlis.append(math.tan(45))
5 nlis.append(math.tan(60))
6 nlis.append(math.tan(90))
7 nlis.append(math.tan(120))
8 nlis.append(math.tan(180))
9 nlis.append(math.tan(270))
10 nlis.append(math.tan(360))
11 print(nlis)
tanh() function
Return the hyperbolic tangent of x.
In [214]:
1 nlis = []
2 nlis.append(math.tanh(1))
3 nlis.append(math.tanh(0))
4 nlis.append(math.tanh(-5))
5 nlis.append(math.tanh(math.pi))
6 nlis.append(math.tanh(math.e))
7 nlis.append(math.tanh(math.tau))
8 nlis.append(math.tanh(math.nan))
9 nlis.append(math.tanh(math.inf))
10 print(nlis)
trunc() function
Truncates the Real x to the nearest Integral toward 0.
Returns the truncated integer parts of different numbers
In [218]:
1 nlis = []
2 nlis.append(math.trunc(1))
3 nlis.append(math.trunc(0))
4 nlis.append(math.trunc(-5))
5 nlis.append(math.trunc(0.577))
6 nlis.append(math.trunc(1.618))
7 nlis.append(math.trunc(math.pi))
8 nlis.append(math.trunc(math.e))
9 nlis.append(math.trunc(math.tau))
10 print(nlis)
[1, 0, -5, 0, 1, 3, 2, 6]
ulp() function
Return the value of the least significant bit of the float x.
In [224]:
1 import sys
2 nlis = []
3 nlis.append(math.ulp(1))
4 nlis.append(math.ulp(0))
5 nlis.append(math.ulp(-5))
6 nlis.append(math.ulp(0.577))
7 nlis.append(math.ulp(1.618))
8 nlis.append(math.ulp(math.pi))
9 nlis.append(math.ulp(math.e))
10 nlis.append(math.ulp(math.tau))
11 nlis.append(math.ulp(math.nan))
12 nlis.append(math.ulp(math.inf))
13 nlis.append(math.ulp(-math.inf))
14 nlis.append(math.ulp(float('nan')))
15 nlis.append(math.ulp(float('inf')))
16 x = sys.float_info.max
17 nlis.append(math.ulp(x))
18 print(nlis)