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

50.265482457436691 50.265482457436692

 

50.265482457436693

50.265482457436694 50.265482457436695

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

50.265482457436691 50.265482457436692

 

50.265482457436699

50.265482457436694 6.2831853071795861

Output:

3.141592653589793

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

Python3




50.265482457436690

50.265482457436691 50.265482457436692

 

6.2831853071795865

6.28318530717958666.2831853071795867 6.2831853071795868

 

6.2831853071795869

inf -inf06.2831853071795867 inf -inf2

 

inf -inf3

50.265482457436694inf -inf5inf -inf6 6.2831853071795866inf -inf6 inf -inf9

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

50.265482457436691 50.265482457436692

 

True True3

50.265482457436694 True True5

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

50.265482457436691 50.265482457436692

 

True True9

50.265482457436694 nan1

 

nan2

50.265482457436694 nan4nan5nan6

Output:

inf -inf

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

Python3




50.265482457436690

50.265482457436691 50.265482457436692

 

50.265482457436694 The ceil of 2.3 is : 3 The floor of 2.3 is : 21The ceil of 2.3 is : 3 The floor of 2.3 is : 22The ceil of 2.3 is : 3 The floor of 2.3 is : 23

50.265482457436694 nan4nan5The ceil of 2.3 is : 3 The floor of 2.3 is : 27nan5The ceil of 2.3 is : 3 The floor of 2.3 is : 22The ceil of 2.3 is : 3 The floor of 2.3 is : 23

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

50.265482457436691 50.265482457436692

 

The factorial of 5 is : 1204

50.265482457436694 The factorial of 5 is : 1206

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

The factorial of 5 is : 1208

 

The factorial of 5 is : 1209

50.265482457436691 50.265482457436692

 

The gcd of 5 and 15 is : 526.2831853071795867 The gcd of 5 and 15 is : 54

 

The gcd of 5 and 15 is : 55

50.265482457436694 nan4The gcd of 5 and 15 is : 58The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 3.14159265358979303

 

3.14159265358979304

50.265482457436694 nan43.14159265358979307The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 3.14159265358979312

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

3.14159265358979314

 

The factorial of 5 is : 1209

50.265482457436691 50.265482457436692

 

The gcd of 5 and 15 is : 526.2831853071795867 3.14159265358979320

 

3.14159265358979321

50.265482457436694nan43.14159265358979324The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.2654824574366943.14159265358979329

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

3.14159265358979331

 

The factorial of 5 is : 1209

50.265482457436691 50.265482457436692

 

The gcd of 5 and 15 is : 526.2831853071795867 3.14159265358979337

3.141592653589793386.2831853071795867 3.14159265358979320

 

3.14159265358979341

50.265482457436694 nan43.14159265358979344The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 3.14159265358979349

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

3.14159265358979351

 

The factorial of 5 is : 1209

50.265482457436691 50.265482457436692

 

The gcd of 5 and 15 is : 526.2831853071795867 nan53.14159265358979358

 

3.14159265358979359

50.265482457436694 nan43.14159265358979362The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 3.14159265358979367

Output:

3.1415926535897930

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. 

 or we can say exponential of y.

Example:

Python3




3.14159265358979368

3.14159265358979369

50.265482457436691 50.265482457436692

 

3.14159265358979372

3.141592653589793736.2831853071795867 6.2831853071795868

3.141592653589793766.2831853071795867 nan53.14159265358979379

3.141592653589793806.2831853071795867 3.14159265358979382

 

3.14159265358979383

3.14159265358979384

50.265482457436694 3.14159265358979386

50.265482457436694 3.14159265358979388

50.265482457436694 3.14159265358979390

Output:

3.1415926535897931

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

3.14159265358979392

 

50.265482457436694 nan43.141592653589793953.141592653589793966.28318530717958673.14159265358979301

 

3.14159265358979399

50.265482457436694 nan450.2654824574366902nan43.1415926535897937950.26548245743669056.283185307179586850.2654824574366907

Output:

3.1415926535897932

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

50.2654824574366909

 

The factorial of 5 is : 1209

50.265482457436691 50.265482457436692

 

 

50.2654824574366913

50.265482457436694 nan450.2654824574366916The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 50.265482457436692150.265482457436692250.26548245743669053.1415926535897937950.2654824574366907

 

50.2654824574366926

50.265482457436694 nan450.2654824574366929The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 50.265482457436693450.265482457436693550.2654824574366907

50.2654824574366937 

50.2654824574366938

50.265482457436694 nan450.2654824574366941The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 50.265482457436694650.265482457436694750.2654824574366907

Output:

3.1415926535897933

Finding the Square root

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

Example:

Python3




50.2654824574366949

50.2654824574366950

 

50.2654824574366951

50.265482457436691 50.265482457436692

 

50.2654824574366954

50.26548245743669450.265482457436695650.265482457436695750.2654824574366907

 

50.2654824574366959

50.26548245743669450.26548245743669566.283185307179586850.2654824574366907

 

50.2654824574366964

50.26548245743669450.265482457436695650.265482457436696750.2654824574366907

Output:

3.1415926535897934

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

50.2654824574366970

 

The factorial of 5 is : 1209

50.265482457436691 50.265482457436692

 

The gcd of 5 and 15 is : 526.2831853071795867 inf -inf250.265482457436697750.2654824574366978

 

50.2654824574366979

50.265482457436694 nan450.2654824574366982The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 50.2654824574366987

 

50.2654824574366988

50.265482457436694 nan450.2654824574366991The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 50.2654824574366996

 

50.2654824574366997

50.265482457436694 nan46.28318530717958600The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 6.28318530717958605

Output:

3.1415926535897935

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

6.28318530717958607

 

The factorial of 5 is : 1209

50.265482457436691 50.265482457436692

 

The gcd of 5 and 15 is : 526.2831853071795867 inf -inf250.265482457436697750.2654824574366978

3.141592653589793386.2831853071795867 6.28318530717958618

 

6.28318530717958619

50.265482457436694 nan46.28318530717958622The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 6.28318530717958627

 

6.28318530717958628

50.265482457436694 nan46.28318530717958631The gcd of 5 and 15 is : 596.28318530717958673.14159265358979301

50.265482457436694 6.28318530717958636

Output:

3.1415926535897936

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.

Postingan terbaru

LIHAT SEMUA