Apa itu library math python?

Sometimes when working with some kind of financial or scientific projects it becomes necessary to implement mathematical calculations in the project. Python provides the math module to deal with such calculations. Math module provides functions to deal with both basic operations such as addition(+), subtraction(-), multiplication(*), division(/) and advance operations like trigonometric, logarithmic, exponential functions.

In this article, we learn about the math module from basics to advance using the help of a huge dataset containing functions explained with the help of good examples.

Constants provided by the math module

Math module provides various the value of various constants like pi, tau. Having such constants saves the time of writing the value of each constant every time we want to use it and that too with great precision. Constants provided by the math module are – 

  • Euler’s Number
  • Pi
  • Tau
  • Infinity
  • Not a Number (NaN)

Let’s see each constant in detail.

Euler’s Number 

The math.e constant returns the Euler’s number: 2.71828182846.

Syntax:

math.e

Example:

Python3




50.26548245743669
0

50.26548245743669
1
50.26548245743669
2

 

50.26548245743669
3

50.26548245743669
4
50.26548245743669
5

Output:

2.718281828459045

Pi

You all must be familiar with pi. The pi is depicted as either 22/7 or 3.14. math.pi provides a more precise value for the pi.

Syntax:

math.pi

Example 1:

Python3




50.26548245743669
0

50.26548245743669
1
50.26548245743669
2

 

50.26548245743669
9

50.26548245743669
4
6.283185307179586
1

Output:

3.141592653589793

Example 2: Let’s find the area of the circle 

Python3




50.26548245743669
0

50.26548245743669
1
50.26548245743669
2

 

6.283185307179586
5

6.283185307179586
6
6.283185307179586
7
6.283185307179586
8

 

6.283185307179586
9

inf
-inf
0
6.283185307179586
7
inf
-inf
2

 

inf
-inf
3

50.26548245743669
4
inf
-inf
5
inf
-inf
6
6.283185307179586
6
inf
-inf
6
inf
-inf
9

Output:

50.26548245743669

Tau

Tau is defined as the ratio of the circumference to the radius of a circle. The math.tau constant returns the value tau: 6.283185307179586.

Syntax:

math.tau

Example:

Python3




50.26548245743669
0

50.26548245743669
1
50.26548245743669
2

 

True
True
3

50.26548245743669
4
True
True
5

Output:

6.283185307179586

Infinity

Infinity basically means something which is never-ending or boundless from both directions i.e. negative and positive. It cannot be depicted by a number. The math.inf constant returns of positive infinity. For negative infinity, use -math.inf.

Syntax:

math.inf

Example 1:

Python3




50.26548245743669
0

50.26548245743669
1
50.26548245743669
2

 

True
True
9

50.26548245743669
4
nan
1

 

nan
2

50.26548245743669
4
nan
4
nan
5
nan
6

Output:

inf
-inf

Example 2: Comparing the values of infinity with the maximum floating point value

Python3




50.26548245743669
0

50.26548245743669
1
50.26548245743669
2

 

50.26548245743669
4
The ceil of 2.3 is : 3
The floor of 2.3 is : 2
1
The ceil of 2.3 is : 3
The floor of 2.3 is : 2
2
The ceil of 2.3 is : 3
The floor of 2.3 is : 2
3

50.26548245743669
4
nan
4
nan
5
The ceil of 2.3 is : 3
The floor of 2.3 is : 2
7
nan
5
The ceil of 2.3 is : 3
The floor of 2.3 is : 2
2
The ceil of 2.3 is : 3
The floor of 2.3 is : 2
3

Output:

True
True

NaN

The math.nan constant returns a floating-point nan (Not a Number) value. This value is not a legal number. The nan constant is equivalent to float(“nan”).

Example:

Python3




50.26548245743669
0

50.26548245743669
1
50.26548245743669
2

 

The factorial of 5 is : 120
4

50.26548245743669
4
The factorial of 5 is : 120
6

Output:

nan

Numeric Functions

In this section, we will deal with the functions that are used with number theory as well as representation theory such as finding the factorial of a number.

Finding the ceiling and the floor value

Ceil value means the smallest integral value greater than the number and the floor value means the greatest integral value smaller than the number. This can be easily calculated using the ceil() and floor() method respectively.

Example:

Python3




The factorial of 5 is : 120
7

The factorial of 5 is : 120
8

 

The factorial of 5 is : 120
9

50.26548245743669
1
50.26548245743669
2

 

The gcd of 5 and 15 is : 5
2
6.283185307179586
7
The gcd of 5 and 15 is : 5
4

 

The gcd of 5 and 15 is : 5
5

50.26548245743669
4
nan
4
The gcd of 5 and 15 is : 5
8
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
3.141592653589793
03

 

3.141592653589793
04

50.26548245743669
4
nan
4
3.141592653589793
07
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
3.141592653589793
12

Output:

The ceil of 2.3 is : 3
The floor of 2.3 is : 2

Finding the factorial of the number

Using the factorial() function we can find the factorial of a number in a single line of the code. An error message is displayed if number is not integral.

Example:

Python3




The factorial of 5 is : 120
7

3.141592653589793
14

 

The factorial of 5 is : 120
9

50.26548245743669
1
50.26548245743669
2

 

The gcd of 5 and 15 is : 5
2
6.283185307179586
7
3.141592653589793
20

 

3.141592653589793
21

50.26548245743669
4
nan
4
3.141592653589793
24
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
3.141592653589793
29

Output:

The factorial of 5 is : 120

Finding the GCD

gcd() function is used to find the greatest common divisor of two numbers passed as the arguments. 

Example:

Python3




The factorial of 5 is : 120
7

3.141592653589793
31

 

The factorial of 5 is : 120
9

50.26548245743669
1
50.26548245743669
2

 

The gcd of 5 and 15 is : 5
2
6.283185307179586
7
3.141592653589793
37

3.141592653589793
38
6.283185307179586
7
3.141592653589793
20

 

3.141592653589793
41

50.26548245743669
4
nan
4
3.141592653589793
44
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
3.141592653589793
49

Output:

The gcd of 5 and 15 is : 5

Finding the absolute value

fabs() function returns the absolute value of the number.

Example:

Python3




The factorial of 5 is : 120
7

3.141592653589793
51

 

The factorial of 5 is : 120
9

50.26548245743669
1
50.26548245743669
2

 

The gcd of 5 and 15 is : 5
2
6.283185307179586
7
nan
5
3.141592653589793
58

 

3.141592653589793
59

50.26548245743669
4
nan
4
3.141592653589793
62
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
3.141592653589793
67

Output:

3.141592653589793
0

Refer to the below article to get detailed information about the numeric functions.

  • Mathematical Functions in Python | Set 1 (Numeric Functions)

Logarithmic and Power Functions

Power functions can be expressed as x^n where n is the power of x whereas logarithmic functions are considered as the inverse of exponential functions.

Finding the power of exp

exp() method is used to calculate the power of e i.e. 

Apa itu library math python?
 or we can say exponential of y.

Example:

Python3




3.141592653589793
68

3.141592653589793
69

50.26548245743669
1
50.26548245743669
2

 

3.141592653589793
72

3.141592653589793
73
6.283185307179586
7
6.283185307179586
8

3.141592653589793
76
6.283185307179586
7
nan
5
3.141592653589793
79

3.141592653589793
80
6.283185307179586
7
3.141592653589793
82

 

3.141592653589793
83

3.141592653589793
84

50.26548245743669
4
3.141592653589793
86

50.26548245743669
4
3.141592653589793
88

50.26548245743669
4
3.141592653589793
90

Output:

3.141592653589793
1

Finding the power of a number

pow() function computes x**y. This function first converts its arguments into float and then computes the power.

Example:

Python3




3.141592653589793
91

3.141592653589793
92

 

50.26548245743669
4
nan
4
3.141592653589793
95
3.141592653589793
96
6.283185307179586
7
3.141592653589793
01

 

3.141592653589793
99

50.26548245743669
4
nan
4
50.26548245743669
02
nan
4
3.141592653589793
79
50.26548245743669
05
6.283185307179586
8
50.26548245743669
07

Output:

3.141592653589793
2

Finding the Logarithm

  • log() function returns the logarithmic value of a with base b. If the base is not mentioned, the computed value is of the natural log.
  • log2(a) function computes value of log a with base 2. This value is more accurate than the value of the function discussed above.
  • log10(a) function computes value of log a with base 10. This value is more accurate than the value of the function discussed above.

Python3




The factorial of 5 is : 120
7

50.26548245743669
09

 

The factorial of 5 is : 120
9

50.26548245743669
1
50.26548245743669
2

 

 

50.26548245743669
13

50.26548245743669
4
nan
4
50.26548245743669
16
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
50.26548245743669
21
50.26548245743669
22
50.26548245743669
05
3.141592653589793
79
50.26548245743669
07

 

50.26548245743669
26

50.26548245743669
4
nan
4
50.26548245743669
29
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
50.26548245743669
34
50.26548245743669
35
50.26548245743669
07

50.26548245743669
37 

50.26548245743669
38

50.26548245743669
4
nan
4
50.26548245743669
41
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
50.26548245743669
46
50.26548245743669
47
50.26548245743669
07

Output:

3.141592653589793
3

Finding the Square root

sqrt() function returns the square root of the number. 

Example:

Python3




50.26548245743669
49

50.26548245743669
50

 

50.26548245743669
51

50.26548245743669
1
50.26548245743669
2

 

50.26548245743669
54

50.26548245743669
4
50.26548245743669
56
50.26548245743669
57
50.26548245743669
07

 

50.26548245743669
59

50.26548245743669
4
50.26548245743669
56
6.283185307179586
8
50.26548245743669
07

 

50.26548245743669
64

50.26548245743669
4
50.26548245743669
56
50.26548245743669
67
50.26548245743669
07

Output:

3.141592653589793
4

Refer to the below article to get detailed information about the Logarithmic and Power Functions

  • Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)

Trigonometric and Angular Functions

You all must know about Trigonometric and how it may become difficult to find the values of sine and cosine values of any angle. Math module provides built-in functions to find such values and even to change the values between degrees and radians.

Finding sine, cosine, and tangent

sin(), cos(), and tan() functions returns the sine, cosine, and tangent of value passed as the argument. The value passed in this function should be in radians.

Example:

Python3




The factorial of 5 is : 120
7

50.26548245743669
70

 

The factorial of 5 is : 120
9

50.26548245743669
1
50.26548245743669
2

 

The gcd of 5 and 15 is : 5
2
6.283185307179586
7
inf
-inf
2
50.26548245743669
77
50.26548245743669
78

 

50.26548245743669
79

50.26548245743669
4
nan
4
50.26548245743669
82
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
50.26548245743669
87

 

50.26548245743669
88

50.26548245743669
4
nan
4
50.26548245743669
91
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
50.26548245743669
96

 

50.26548245743669
97

50.26548245743669
4
nan
4
6.283185307179586
00
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
6.283185307179586
05

Output:

3.141592653589793
5

Converting values from degrees to radians and vice versa

  • degrees() function is used to convert argument value from radians to degrees.
  • radians() function is used to convert argument value from degrees to radians.

Example:

Python3




The factorial of 5 is : 120
7

6.283185307179586
07

 

The factorial of 5 is : 120
9

50.26548245743669
1
50.26548245743669
2

 

The gcd of 5 and 15 is : 5
2
6.283185307179586
7
inf
-inf
2
50.26548245743669
77
50.26548245743669
78

3.141592653589793
38
6.283185307179586
7
6.283185307179586
18

 

6.283185307179586
19

50.26548245743669
4
nan
4
6.283185307179586
22
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
6.283185307179586
27

 

6.283185307179586
28

50.26548245743669
4
nan
4
6.283185307179586
31
The gcd of 5 and 15 is : 5
9
6.283185307179586
7
3.141592653589793
01

50.26548245743669
4
6.283185307179586
36

Output:

3.141592653589793
6

Refer to the below articles to get detailed information about the trigonometric and angular functions.

  • Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)

Special Functions

Besides all the numeric, logarithmic functions we have discussed yet, the math module provides some more useful functions that does not fall under any category discussed above but may become handy at some point while coding.