Python
In this tutorial we will learn about Math module and Mathematical Functions in Python.
The math module is a standard module available in Python and it provides mathematical functions to developers.
To use the functions available in the math module we must start by first including the module using the import statement.
import
Write the following line to import the math module.
import math
Following are the commonly used math constants used in Python.
e
pi
In the following example we are printing the value of the mathematical constants.
# import module import math # output print("e:", math.e) # e: 2.718281828459045 print("pi:", math.pi) # pi: 3.141592653589793
Following are some of the commonly used mathematical functions from the math module in Python.
math.ceil(x)
math.fabs(x)
math.floor(x)
math.log(x)
math.log10(x)
max(x1, x2, ...)
min(x1, x2, ...)
math.pow(x, y)
round(x, [,n])
math.sqrt(x)
In the following Python program we are using some of the Math functions.
# import module import math # output print("ceil:", math.ceil(3.14)) # ceil: 4 print("fabs:", math.fabs(3.14)) # fabs: 3.14 print("fabs:", math.fabs(-3.14)) # fabs: 3.14 print("floor:", math.floor(-3.14)) # fabs: -4 print("log:", math.log(8)) # log: 2.0794415416798357 print("log10:", math.log10(8)) # log10: 0.9030899869919435 print("max:", max(1, 5, 3, 4)) # max: 5 print("min:", min(9, 3, 1, 6)) # min: 1 print("pow:", math.pow(2, 10)) # pow: 1024 print("round:", round(3.14159)) # round: 3 print("round:", round(3.14159, 2)) # round: 3.14 print("sqrt:", math.sqrt(4)) # sqrt: 2
Following are some of the commonly used trigonometry functions from the math module in Python.
math.acos(x)
math.asin(x)
math.atan(x)
math.cos(x)
math.sin(x)
math.tan(x)
math.degrees(x)
math.radians(x)
In the following Python program we are using some of the trigonometry functions.
# import module import math # output print("acos:", math.acos(0.5)) # acos: 1.0471975511965976 print("asin:", math.asin(0.5)) # asin: 0.5235987755982988 print("atan:", math.atan(0.5)) # atan: 0.46364760900080615 print("cos:", math.cos(1.047197551)) # cos: 0.5000000001702587 print("sin:", math.sin(0.523598775)) # sin: 0.49999999948185797 print("tan:", math.tan(0.463647609)) # tan: 0.4999999999989924 print("degree:", math.degrees(3.1415926)) # degree: 179.99999692953102 print("radian:", math.radians(180)) # radian: 0.4999999999989924