Python – Mathematics
Python – Mathematics:
In python, we have the module called “math” which provides the access to mathematical functions which are defined in C programming.
Doing mathematics using python is always a fun.
There are 2 types of modules for mathematical functions.
- math – This is used for normal numbers
- cmath – This is used for complex numbers
There are different types of mathematical functions available in math module.
- Number Functions
- Power and logarthmic Functions
- Trigonometric Functions
- Angular conversion
- Hyperbolic Functions
- Special Functions
- Constants
Examples of Number Functions:
>>> from math import ceil, factorial, floor, gcd, fsum, trunc # ceiling the value >>>ceil(10.3) 11 >>>ceil(9.9) 10 # Factorial of given value >>>factorial(3) 6 >>>factorial(10) 3628800 # Floor of value given >>>floor(10.3) 10 >>>floor(10.9) 10 # calculates the GCD of given numbers >>>gcd(5,10)
5 >>>gcd(3,7) 1
# Floating point sum of of values >>>fsum([5,4,5,1]) 15.0 # Truncating the values >>>trunc(9.4) 9 >>>trunc(10.5) 10 >>> Examples of Power and logarthmic Functions:
>>>math.exp(2) 7.38905609893065 >>>math.log(2,10) 0.30102999566398114 >>>math.log(2,4) 0.5 >>> math.log2(4) 2.0 >>> math.log10(2)
0.3010299956639812 >>math.pow(2,3) 8.0 >>>math.sqrt(64) 8.0
Examples of Trigonometric Functions:
>>> from math import sin, cos, tan # Values are in radians >>>sin(30) -0.9880316240928618 >>>cos(90) -0.4480736161291701 >>>tan(0) 0.0
Examples of Angular conversion:
>>> from math import degrees,radians >>>degrees(10) 572.9577951308232 >>>radians(572) 9.983283321407566
Share: