Math

Number

In [ ]:
#!/usr/bin/python
import math
 
#ceil
print(math.ceil(3.14)) # 4
 
#fabs, replace abs
print(math.fabs(-3.14)) # 3.14
 
#factorial
print(math.factorial(10)) # 3628800
 
#floor
print(math.floor(3.14)) # 3
 
#fmod, replace %
print(math.fmod(10, 3)) # 1.0
 
#fsum, replace sum
print(math.fsum(range(10))) # 45.0
 
#round, is not a function of math module
print(round(3.14, 1)) # 3.1
 
#trunc
print(math.trunc(3.14)) # 3

Power and Log

In [ ]:
#!/usr/bin/python
import math
 
#exp
print(math.exp(2)) # 7.389
 
#log
print(math.log(100, 10)) # 2.0
 
#pow
print(math.pow(10, 2)) # 100.0
 
#sqrt
print(math.sqrt(100)) # 10.0

Angle

In [ ]:
#!/usr/bin/python
import math

#degrees, convert radian to degree
print(math.degrees(3.14)) # 179.0

#radians, convert degree to radians
print(math.radians(180)) # 3.14149

#sin
print(math.sin(math.radians(180))) # 1.22e-16

#cos
print(math.cos(math.radians(180))) # -1.0

#acos
print(math.degrees(math.acos(0))) # 90.0

Constants

In [ ]:
#!/usr/bin/python
import math
 
#pi
print(math.pi) # 3.141592653589793
 
#e
print(math.e) # 2.718281828459045