Python Math Module Tutorial

In Python, the math module is an integrated module that provides access to mathematical functions, enabling us to execute intricate mathematical computations, including logarithmic functions, exponential calculations, and various other arithmetic operations.

To read more Python Modules

Let us examine a straightforward illustration to grasp the application of the Math module:

Example: Finding Square Root

Example

Example

#Importing the math module

import math

#Entering the number

num = int(input("Enter a number:"))

#We are sqrt() function to find the square root of the number

print(math.sqrt(num))  # Uses math module

Output:

Output

Enter a number:25

The square root of the number is: 5.0

Explanation

In the aforementioned example, we have included the math module due to its built-in function sqrt, which is utilized to calculate the square root.

Constants in math Module

The math module offers the numerical values of various constants that are commonly utilized in mathematical scenarios.

For instance, to calculate the area of a circle, it is essential to utilize pi, as the formula is expressed as pi*r², where r denotes the radius of the circle.

Thus, the math module simplifies our work by offering predefined constant values.

Constant Values Definition
Pi(π) The value of pi(π) is 22/7 or 3.14 mathematically, we use math.pi to get the most accurate value.
Euler's number(e) The mathematically accepted value of Euler's number, which is returned by math.e is 2.718281828459045.
Tau(τ) The value of Tau, which is returned by math.tau is 6.283185307179586.
Infinity(∞) The Mathematical value of Infinity, which is returned by math.inf, is inf. The infinity can be both Positive and Negative.
Not a Number(NaN) The value of Not a Number(NaN), which is returned by math.nan, is nan, which is not a valid number.

Example: Printing Constant Values

Example

Example

#importing math module

import math

#Printing constant values

print("Value of Pi Constant=", math.pi)		 #Pi's constant

print("Value of Euler's Constant=", math.e)   #Euler's constant

print("Value of Tau's Constant=", math.tau)   #Tau's constant

print("Value of Infinity =", math.inf)        #Infinity

print("Value of Not a Number =", math.nan)    #Not a Number

Output:

Output

Value of Pi Constant 3.141592653589793

Value of Euler's Constant 2.718281828459045

Value of Tau's Constant 6.283185307179586

Value of Infinity = inf

Value of Not a Number = nan

Explanation

In the example provided, we have included the math module and displayed the values of different mathematical constants by utilizing math.constant_name.

Python math Module Functions

The Python math module provides a means to execute sophisticated mathematical computations, including trigonometric functions, logarithmic calculations, and exponential operations. To facilitate these intricate calculations, the math module offers a variety of built-in functions.

S. N. Functions Description
1 math.sin(x) It provides the sin value of x
2 math.cos(x) It gives the cosine value of x.
3 math.tan(x) It produces the tangent value of x.
4 math.asin(x) It provides the arc sine value of x.
5 math.acos(x) This function gives arc cosine value of x.
6 math.atan(x) This function gives the arc tangent value of x (in radians).
7 math.atan2(y, x) It provides the arc tangent value of y/x in radians.
8 math.sinh(x) Hyperbolic sin value.
9 math.cosh(x) Hyperbolic cosine value.
10 math.tanh(x) Hyperbolic tangent value.
11 math.asinh(x) Inverse hyperbolic sine value.
12 math.acosh(x) Inverse hyperbolic cosine value.
13 math.atanh(x) Inverse hyperbolic tangent value.
14 math.degrees(x) It converts radians to degrees.
15 math.radians(x) It converts degrees to radians.
16 math.exp(x) It gives ex.
17 math.expm1(x) It gives ex-1.
18 math.log(x, base) It gives the value of log with its base.
19 math.log10(x) It gives the Base 10 logarithm.
20 math.log1p(x) log(1+x).
21 math.log2(x) It gives the Base 2 logarithm.
22 math.pow(x, y) It gives xy.
23 math.sqrt(x) This generates the square root of the number.
24 math.fabs(x) It gives the absolute value.
25 math.factorial(n) It gives the factorial of number n.
26 math.comb(x, y) It produces the combinations.
27 math.perm(x, y) It produces the permutations.
28 math.isfinite(n) It checks if n is finite.
29 math.isinf(n) It checks if n is infinite.
30 math.gamma(x) It returns the gamma function of the argument.
31 math.lgamma(x) It returns the natural log of the gamma function.

Example 1: Finding the Factorial of a Number

In the following example, we will demonstrate how to compute the factorial of a given number utilizing the math module.

Example

Example

#importing the math module

import math

n = int(input("Enter a number: "))

#using math.factorial() function

print("The factorial of the entered number is: ",math.factorial(n))

Output:

Output

Enter a number: 5

The factorial of the entered number is:  120

Explanation

In this illustration, we determined the factorial of a given number by utilizing the math.factorial(n) function, where n represents the number provided by the user.

Performing Trigonometric Operations

The subsequent illustration shows the procedure for determining trigonometric ratios by utilizing the math module.

Example: Using Trigonometric Operations

Example

Example

#importing the math module

import math

#taking input from the user

n = int(input("Enter a number: "))

#returning the values of various trigonometric functions

print("The Sine value is: ",math.sin(n))

print("The Cosine value is: ",math.cos(n))

print("The Tan value is: ",math.tan(n))

Output:

Output

Enter a number: 90

The Sine value is:  0.8939966636005579

The Cosine value is:  -0.4480736161291701

The Tan value is:  -1.995200412208242

Explanation

In the preceding example, we computed the values for several trigonometric functions by utilizing the sin, cos, and tan functions available in the math module.

Permutation and Combination

In the subsequent example, we will calculate the number of methods to select a specific quantity of balls from a collection of balls. To compute the combinations, we will utilize the math.comb function.

Example: Ways of Choosing Balls from a Bag

Example

Example

#importing the math module

import math

# given data

n = 10	# total no of balls

r = 4		# number of balls to be selected

# Finding the number of combinations using math.comb()

no_of_ways = math.comb(n, r)

# printing result

print("Given Data:")

print("Total number of Balls in the bag:", n)

print("Number of Balls to be selected:", r)

print("Total number of Ways to select", r, "balls from the bag of", n, "balls:")

print(no_of_ways)

Output:

Output

Given Data:

Total number of Balls in the bag: 10

Number of Balls to be selected: 4

Total number of Ways to select 4 balls from the bag of 10 balls:

210

Explanation

In this section, we determined the various methods to select 4 balls from a collection of 10 balls by utilizing the math.comb function.

Example 4: Degree to Radians and Vice-Versa Conversion

In this section, we will transform radians into degrees and degrees into radians by gathering input from the user.

Example

Example

#importing the math module

import math

# Taking input in radians and converting to degrees

rad = float(input("Enter the angle in radians: "))

print("Radians to Degrees =", math.degrees(rad))

# Taking input in degrees and converting to radians

deg = float(input("Enter the angle in degrees: "))

print("Degrees to Radians =", math.radians(deg))

Output:

Output

Enter the angle in radians: 6.28318530718

Radians to Degrees = 360.0000000000237

Enter the angle in degrees: 360

Degrees to Radians = 6.283185307179586

Explanation

In this illustration, we have utilized the functions math.degrees and math.radians to transform radians into degrees and the other way around, while also obtaining input from the user.

Calculating Exponential

The expression x raised to the power of e, commonly referred to as the exponential of the value x, can be determined using the exp function.

Example

Example

# Python program to show how to use the exp() function.

# importing the math module

import math

# declaring some value

num1 = 4

num2 = -3

num3 = 0.00

# passing above values to the exp() function

print( f"The exponenetial value of {num1} is: ", math.exp(num1) )

print( f"The exponenetial value of {num2} is: ", math.exp(num2) )

print( f"The exponenetial value of {num3} is: ", math.exp(num3) )

Output:

Output

The exponenetial value of 4 is: 54.598150033144236

The exponenetial value of -3 is: 0.049787068367863944

The exponenetial value of 0.0 is: 1.0

Using dir Function

The built-in function dir produces a sorted array of strings that represent the identifiers for the functions that have been defined within a particular module.

The compilation features the titles of modules, along with their defined constants, functions, and methods. Below is a clear example:

Example

Example

# Importing the math module

import math

functions = dir(math)

print( functions )

Output:

Output

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', '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']

Conclusion

The Python Math Module is an integrated module that enables users to carry out sophisticated mathematical computations, including trigonometric functions, logarithmic calculations, and exponential operations. We explored different categories of functions, including those that are exponential and logarithmic in nature. Additionally, we examined constant values and discussed their practical uses.

Python Math Module FAQs

1. How to use the math module?

To utilize the math module in your Python script, you can import it by entering the command import math.

2. What are some common functions in the Python Math Module?

There are various functions available in the Python Math module, such as:

  • exp(x) : It gives e x
  • sqrt(x) : It gives the square root of the number x
  • factorial(n) : It gives the factorial of the number n.

The Python Math Module encompasses a variety of additional functions; kindly refer to the list provided above.

3. What constants does the math module provide?

The mathematics modules include a variety of constants, including Pi (π), Tau (τ), Euler's number (e), Infinity (∞), and NaN (Not a Number).

4. Can we use complex numbers in the Math Module?

To perform calculations with complex numbers, it is necessary to import and utilize the cmath module.

5. What is the difference between math.pow and the ** operator?

The Math.pow function and the operator are quite alike, but there exists a minor distinction between them: the Math.pow method yields a floating-point value, while the operator generates an integer output.

Input Required

This code uses input(). Please provide values below: