Python math Module

Python-Programming
Extension Description
Built-in module for mathematical functions for programming in Python.

Introduction

Python Math Module is a built-in module that contains a large number of mathematical functions. This module is useful for dealing with mathematics-related tasks such as statistics, complex numbers, etc. The math module will also provide useful mathematical constants such as pi.

Definition and Usecase

The math module defines a wide range of useful mathematical functions that can be used directly in any Python program. The use case for Math Module ranges from basic arithmetic operations (such as addition, subtraction, division, etc.), to more complex operations (such as trigonometric functions, complex numbers, etc.).Examples with Output

Examples with Output

Example 1

Below is an example of using the math module to calculate the absolute value of a number.

import math 

# initializing the number 
number = -2.45 

# calculating absolute value of number 
absolute_value = math.fabs(number) 

# printing the absolute value 
print("Absolute value of",number,"is",absolute_value)

Output:

Absolute value of -2.45 is 2.45

Example 2

Below is the example of using math module to calculate the natural logarithm of a number.

import math

# initializing the number
number = 50

# calculating natural log of number
natural_log = math.log(number)

# printing the natural log of number
print("The natural log of", number, "is", natural_log)

Output:

The natural log of 50 is 3.912023005428146

Read More

Python Functions

The function returns the arc cosine of a number.
Syntax: math.acos(x)
The function is used to return the inverse hyperbolic cosine of a given value.
Syntax: math.acosh(x)
The function is used to calculate the arcsine (inverse of the sine function) of the given value and return the arcsine in radians.
Syntax: math.asin(x)
The function is a unary operation that calculates the inverse hyperbolic sine of a given value.
Syntax: math.asinh(x)
The function is used to compute the arc tangent of the given angle in radians.
Syntax: math.atan(x)
The function takes two arguments – y-coordinate and x-coordinate – and produces an angle in radians from -(pi) to +(pi).
Syntax: math.atan2(y, x)
The function is used to calculate the inverse hyperbolic tangent of a given value.
Syntax: math.atanh(x)
The function is used to return the ceiling value of a number, which means it is used to round a number up to the nearest integer that is greater than the number itself.
Syntax: math.ceil(x)
All articles loaded
No more articles to load
Table of Contents