Math Module In Python

aimtocode

Python Math Module

A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use.

A module is a file that contains a collection of related functions. Python has lot of built-in modules; math module is one of them.

math module provides most of the familiar mathematical functions.

These include trigonometric functions, representation functions, logarithmic functions, angle conversion functions, etc.

Before we can use the functions in a module, we have to import it with an import statement:

syntax:

 import math

Example:

 import math

 print("Pi: %f" % (math.pi))
 print("e: %f" % (math.e))

Output:

 Pi: 3.141593
 e: 2.718282

math module methods

ceil() and floor()

All of these functions are related to rounding. Ceil() always rounds upwards and floor() rounds downwards no matter what.

Example:

 import math

 print(round(3.1))
 print(round(3.6))
 print(math.ceil(3.1))
 print(math.floor(3.6))

Output:

 3
 4
 4
 3

math.log()

This method is used to convert the natural logarithm of a given number. It can be calculated to the base e.

Example:

 import math

 print("math.log(10.43):", math.log(10.43))
 print("math.log(20):", math.log(20))
 print("math.log(math.pi):", math.log(math.pi))

In the above Example, we are passing numeric values with different data types to the method. We have also calculated the natural logarithm of the pi constant.

Output:

 math.log(10.43): 2.344686269012681
 math.log(20): 2.995732273553991
 math.log(math.pi): 1.1447298858494002

math.log10()

The math.log10() method returns the base-10 logarithm of the given number. It is called the standard logarithm.

import math  
x=13  # small value of of x  
print('log10(x) is :', math.log10(x))

Output:

log10(x) is : 1.1139433523068367

Some of the most commonly used math functions in Python.

Function Description
math.ceil(x) Returns the smallest integer greater than or equal to x.
math.fabs(x) Returns the absolute value of x.
math.floor(x) Returns the largest number not greater that x.
math.log(x) Finds the natural log of x (where, x > 0).
math.log10(x) Finds the base 10 log of x (where, x > 0).
max(x1, x2, ...) Returns the maximum value from the given values.
min(x1, x2, ...) Returns the minimum value from the given values.
math.pow(x, y) Returns x to the power of y i.e. xy.
round(x, [,n]) Returns rounded value of x upto n decimal place.
math.sqrt(x) Returns the square root of x.

Examples:



Output:

 ceil: 4
 fabs: 3.14
 fabs: 3.14
 floor: -4
 log: 2.0794415416798357
 log10: 0.9030899869919435
 max: 5
 min: 1
 pow: 1024.0
 round: 3
 round: 3.14
 sqrt: 2.0


Math Trigonometry Functions

Some of the most commonly used trigonometry functions in Python.

Function Description
math.acos(x) Returns the arc cosine of x radian.
math.asin(x) Returns the arc sine of x radian.
math.atan(x) Returns the arc tangent of x radian.
math.cos(x) Returns the cosine of x radian.
math.sin(x) Returns the sine of x radian.
math.tan(x) Returns the tangent of x radian.
math.degrees(x) Convert x from radian to degree.
math.radians(x) Convert x from degree to radian.

Example:



Output:

 acos: 1.0471975511965979
 asin: 0.5235987755982989
 atan: 0.4636476090008061
 cos: 0.5000000001702586
 sin: 0.4999999994818579
 tan: 0.49999999999899236
 degree: 179.99999692953102
 radian: 3.141592653589793
aimtocode