Introduction
In this article we will learn about python maths function. To perform mathematical operations we have math module in Python.
What is Module
In C and C++ Programming we have header files, in which we can have functions, classes variables, etc. By including that header files in our code we don't write more code and reuse our functions. Same like in Python we have modules, which may have functions, classes, variables and compiled code. A module contains group of related functions, classes and variables.
There are three kinds of modules in python:
- Modules written in Python(.py).
- Modules which are written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc).
- Modules which are written in C but linked with interpreter to get the list of all modules which are written in C, but linked with Python you can use the following code.
- import sys
- print(sys.builtin_module_names)
Output:
('_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections', '_csv', '_datetime', '_functools', '_heapq', '_imp', '_io', '_json', '_locale', '_lsprof', '_md5', '_multibytecodec', '_opcode', '_operator', '_pickle', '_random', '_sha1', '_sha256', '_sha512', '_sre', '_stat', '_string', '_struct', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', '_winapi', 'array', 'atexit', 'audioop', 'binascii', 'builtins', 'cmath', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'parser', 'signal', 'sys', 'time', 'winreg', 'xxsubtype', 'zipimport', 'zlib').
Math Module
You can see in the above list, the math module is written in C programming but linked with Interpreter that contains Math functions and variables. which we will discuss in this article.
Number Representational Function
Ceil and floor function
Ceil and floor functions are common functions. The ceil function rounds up the number to the next highest one. The floor function removes digits decimal points. Both function takes decimal number as argument and returns integer.
Example:
-
- import math
-
-
- number=8.10
-
-
- print("Ceiling value of 8.10 is :",math.ceil(number))
-
-
- print("Floor value of 8.10 is :",math.floor(number))
Output:
fabs function
fabs function is used to compute absolute value of any number. If a number contains any negative(-) sign, then it removes that sign and returnss positive fractional number.
Example:
-
- import math
- number = -8.10
-
- print(math.fabs(number))
Output:
factorial function
This functions takes positive integer number and prints factorial of that number.
Example:
-
- import math
- number = -8.10
-
- print(math.fabs(number))
Output:
Note: When you will enter negative number, then you will get a Value Error.
Example:
-
- import math
- number = -5
-
- print("fatorial of the number is",math.factorial(number))
Output:
fmod function
function fmod(x,y) returns x%y. But the difference is x%y preferred when working with integers fmod(x,y) and is used when working with floats.
Example:
-
- import math
- print(math.fmod(5,2))
- print(math.fmod(-5,2))
- print(math.fmod(-5.2,2))
- print(math.fmod(5.2,2))
Output:
frexp function
This function returns mantissa and exponent in the pair(m,n) of any number x by solving the following equation.
Example:
-
- import math
- print(math.frexp(24.8))
Output:
fsum function
It calculates accurate floating point sum of values in iterable and calculates sum of list of data or range of data.
Example:
-
- import math
-
-
- numbers=[.1,.2,.3,.4,.5,.6,.7,.8,8.9]
- print("sum of ",numbers," is :",math.fsum(numbers))
-
-
- print("sum of 1 to 10 numbers is :",math.fsum(range(1,11)))
Output:
Power and Logarithmic Function
exp function
This function takes one parameter as a fractional number and returns e^x.
Example:
-
- import math
-
- print("e ^ 5 is ",math.exp(5))
- print("e ^ 2.5 is",math.exp(2.5))
Output:
expm1 function
This function works same like exp function but returns exp(x)-1. Here expm1 means exp-m-1 that is exp-minus-1.
Example:
-
- import math
-
- print(math.exp(5)-1)
- print(math.expm1(5))
Output:
log function
log function log(x[,base]) finds log of the given number like x with base "e" default. Here base is the optional parameter. If you want to calculate log with specific base then you can specify the base.
Example:
-
- import math
-
-
- print(math.log(2))
-
-
- print(math.log(64,2))
Output:
log1p function
This is similar to log function but adds 1 in log result. log1p means log-1-p that is log-1-plus.
Example:
-
- import math
-
- print(math.log1p(2))
Output:
log10 function
This calculates log with base 10.
Example:
-
- import math
-
- print(math.log10(1000))
Output:
pow function
This is used to find out the power of the number. Syntax of the pow function is pow(Base, Power). It takes 2 arguments ;1st is base and 2nd is power.
Example:
-
- import math
-
- print(math.pow(5,4))
Output:
sqrt function
This function is used to find out the square root of the number. It takes number as a argument and finds square root.
Example:
-
- import math
-
- print(math.sqrt(256))
Output:
Trigonometric Functions
In python we have the following trigonometric functions.
- sin: Takes one parameter in radian and return sine of that radian.
- cos: Takes one parameter in radian and return cosine of that radia.
- tan: Takes one parameter in radian and return tangent of that radian.
- asin: Takes one parameter and returns arc sine(Inverse of sine).
- acos: Takes one parameter and returns arc cosine(Inverse of cosine).
- atan: Takes one parameter and returns arc tangent(inverse of tangent).
- sinh: Takes one parameter and returns hyperbolic sine.
- cosh: Takes one parameter and returns hyperbolic cosine.
- tanh: Takes one parameter and returns hyperbolic tangent.
- asinh: Takes one parameter and returns inverse hyperbolic sine.
- acosh: Takes one parameter and returns inverse hyperbolic cosine.
- atanh: Takes one parameter and returns inverse hyperbolic tangent.
Example:
-
- import math
-
-
- print("sin PI/2 :",math.sin(math.pi/2))
-
- print("cos 0 :",math.cos(0))
-
- print("tan PI/4 :",math.tan(math.pi/4))
-
- print()
-
-
- print("asin 0 :",math.acos(0))
-
- print("acos 1 :",math.acos(1))
-
- print("atan 0.5 :",math.atan(0.5))
-
- print()
-
-
- print("sinh 1 :",math.sinh(1))
-
- print("cosh 0 :",math.cos(0))
-
- print("tanh 1 :",math.tan(1))
-
- print()
-
-
- print("asinh 1 :",math.acosh(1))
-
- print("acosh 1 :",math.acosh(1))
-
- print("atanh 0.5 :",math.atanh(0.5))
Output:
Angular Conversion Function
These functions convert angle. In maths we can write angles in the following two ways: degree and radian. There are two functions in Python which converts degree to radian and vice versa.
- degrees: Converts radian to degrees.
- radians: Converts degree to radians.
Example:
-
- import math
-
- print(math.degrees(1.57))
-
- print(math.radians(90))
Output:
Math Constants
In python there are two math constants. pi and e
- pi : This is the mathematical constant whose value is : 3.1416
- e : This is also the mathematical constant whose value is : 2.7183
Example
-
- import math
-
-
- print("value of PI is",math.pi)
-
- print("value of e is",math.e)
Output
My Other Python Article links