Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

18 MathModuleFunctionsinPython-JupyterNotebook

The document discusses various math module functions in Python including trigonometric functions like acos(), logarithmic functions like log(), and other functions like ceil() and erf(). Example code is provided to demonstrate how each function works by passing different parameter values and displaying the output. Some key functions covered are acos(), log(), factorial(), ceil(), and erf().

Uploaded by

David Barreto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

18 MathModuleFunctionsinPython-JupyterNotebook

The document discusses various math module functions in Python including trigonometric functions like acos(), logarithmic functions like log(), and other functions like ceil() and erf(). Example code is provided to demonstrate how each function works by passing different parameter values and displaying the output. Some key functions covered are acos(), log(), factorial(), ceil(), and erf().

Uploaded by

David Barreto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

15.06.2022 13:55 18.

Math Module Functions in Python - Jupyter Notebook

Math Module Functions in Python


Created by Mustafa Germec, PhD

18. Math Module Functions in Python


Python math module is defined as the most famous mathematical functions, which includes
trigonometric functions, representation functions, logarithmic functions, etc.
Furthermore, it also defines two mathematical constants, i.e., Pie and Euler number, etc.
Pie (n): It is a well-known mathematical constant and defined as the ratio of circumstance to the diameter
of a circle. Its value is 3.141592653589793.
Euler's number(e):It is defined as the base of the natural logarithmic, and its value is
2.718281828459045.
The math module has a set of methods and constants.

In [1]:

1 # Import math module and functions


2 import math
3 from math import *

In [2]:

1 # Many functions regarding math modules in python can be find using helf(math) method.
2 help(math)

Help on built-in module 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.

The result is between 0 and pi.

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.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Modul… 1/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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

[-4.951718775643098, 0.0, 4.951718775643098]

ceil() function
Rounds a number up to the nearest integer
Returns the smalles integer greater than or equal to variable.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Modul… 3/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

In [22]:

1 pi_number = math.pi # math.pi is equal to pi_number 3.14.


2 print(f'The nearest integer greater than pi number is {math.ceil(pi_number)}.')

The nearest integer greater than pi number is 4.

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]:

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

The combination of 6 with 2 is 15.

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

TypeError: 'float' object cannot be interpreted as an integer

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)}.')

The copysign of thes two numbers -3.14 and 2.718 is 3.14.


The copysign of thes two numbers 1729 and -0.577 is -1729.0.

cos() function
Return the cosine of x (measured in radians).

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Modul… 4/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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)

[1.5430806348152437, 1.0, 74.20994852478785]

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)

[90.0, 180.0, 45.0, -180.0]

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)))

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Modul… 5/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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)

[1.0, 0.9999911238536323, 0.9998790689599072, 1.0, 0.0, 1.0, 0.9778739803135315, 0.585500565194


3818, -1.0]

erfc() function
Complementary error function at x.
This method accepts a value between - inf and + inf, and returns a value between 0 and 2.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Modul… 6/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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)

[0.0, 8.876146367641612e-06, 0.00012093104009276267, 6.348191705159502e-19, 1.0, 2.1519736712


498913e-17, 0.022126019686468514, 0.41449943480561824, 2.0]

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)

[inf, 23.140692632779267, 15.154262241479262, 535.4916555247646, 1.0, 403.4287934927351, 5.042


994235377287, 1.780688344599613, 0.0]

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.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Modul… 7/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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)

[inf, 22.140692632779267, 14.154262241479262, 534.4916555247646, 0.0, 402.4287934927351, 4.042


994235377287, 0.7806883445996128, 6.38905609893065, -1.0]

fabs() function
Returns the absolute value of a number

In [14]:

1 print(f'The absolute value of the number -1.618 is {math.fabs(-1.618)}.')

The absolute value of the number -1.618 is 1.618.

factorial() function
Returns the factorial of a number.

In [28]:

1 print(f'The factorial of the number 6 is {math.factorial(6)}.')

The factorial of the number 6 is 720.

In [29]:

1 # Factorial of negative numbers returns a ValueError.


2 print(math.factorial(-6))

---------------------------------------------------------------------------
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))

ValueError: factorial() not defined for negative values

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Modul… 8/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

In [30]:

1 # Factorial of non-unteger numbers returns a TypeError.


2 print(math.factorial(3.14))

---------------------------------------------------------------------------
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))

TypeError: 'float' object cannot be interpreted as an integer

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.)

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Modul… 9/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

In [142]:

1 special_nums = [0.577, 1.618, 2.718, 3.14, 6, 28, 37, 1729]


2 print(math.fsum(special_nums))

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).

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Mod… 10/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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]:

1 print(math.isclose(math.pi, math.tau)) # tau number is 2 times higher than pi number


2 print(math.isclose(3.14, 2.718))
3 print(math.isclose(3.14, 1.618))
4 print(math.isclose(10, 5, rel_tol = 3, abs_tol=0))
5 print(math.isclose(3.14, 3.1400000000001))

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]

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Modu… 11/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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)

[True, False, False, False, False, False, False, False, True]

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.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Mod… 12/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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))

ValueError: isqrt() argument must be nonnegative

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))

TypeError: 'float' object cannot be interpreted as an integer

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)

[75, 54, 378]

ldexp() function
Returns the inverse of math.frexp() which is x*(2^i) of the given numbers x and i

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Mod… 13/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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)

[4.499809670330265, 0.0, 1.0, 1.1447298858494002, 1.8378770664093453, inf, nan]

log10() function
Return the base 10 logarithm of x.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Mod… 14/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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)

[1.954242509439325, 0.0, 0.4342944819032518, 0.49714987269413385, 0.798179868358115, inf, nan]

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)

[4.51085950651685, 0.6931471805599453, 1.3132616875182228, 1.4210804127942926, 1.985568308


7099187, inf, nan]

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)

[6.491853096329675, 1.0, 0.0, 1.4426950408889634, 1.6514961294723187, 2.651496129472319, inf, n


an]

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Mod… 15/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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)

[3.1400000000000006, 5.999999999999999, 2.9999999999999996, 28.000000000000004, nan, 1.0, 0.


0]

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.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Mod… 16/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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]:

1 special_nums = [0.577, 1.618, 2.718, 3.14, 6, 28, 37, 1729]


2 print(math.prod(special_nums))

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)

[0.0, 0.5235987755982988, 0.7853981633974483, 1.0471975511965976, 1.5707963267948966, 2.0943


951023931953, 3.141592653589793, 4.71238898038469, 6.283185307179586]

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.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Mod… 17/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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)

[0.42200000000000015, 6.0, -1.0, -10.0]

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)

[1.2246467991473532e-16, 1.0, 0.41078129050290885, nan, -2.4492935982947064e-16, -0.988031624


0928618, 0.9589242746631385, -0.6435381333569995]

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)

[1.1752011936438014, 0.0, -74.20321057778875, 11.548739357257746, 7.544137102816975, 267.744


89404101644, nan, inf]

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

Return the square root of x.

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)

[1.0, 0.0, 6.082762530298219, 1.7724538509055159, 1.6487212707001282, 2.5066282746310002, na


n, inf]

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)

[0.0, -6.405331196646276, 1.6197751905438615, 0.320040389379563, -1.995200412208242, 0.71312


30097859091, 1.3386902103511544, -0.17883906379845224, -3.380140413960958]

tanh() function
Return the hyperbolic tangent of x.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Mod… 19/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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)

[0.7615941559557649, 0.0, -0.9999092042625951, 0.99627207622075, 0.9913289158005998, 0.99999


30253396107, nan, 1.0]

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.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Mod… 20/21


15.06.2022 13:55 18. Math Module Functions in Python - Jupyter Notebook

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)

[2.220446049250313e-16, 5e-324, 8.881784197001252e-16, 1.1102230246251565e-16, 2.2204460492


50313e-16, 4.440892098500626e-16, 4.440892098500626e-16, 8.881784197001252e-16, nan, inf, inf, n
an, inf, 1.99584030953472e+292]

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/18. Math Mod… 21/21

You might also like