Introduction
What is Module?
In C and C++ Programming we have header files, in which we can have functions, class variables, etc. By including that header files in our code we don't write more code and reuse our functions. Same as in Python we have modules, which may have functions, classes, variables and compiled code. A module contains a group of related functions, classes, and variables.
There are three kinds of modules in python:
- Modules are written in Python(.py).
- Modules which are written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc).
- Modules that are written in C but linked with an interpreter to get the list of all modules which are written in C, but linked with Python you can use the following code.
Output:
- import sys
- print(sys.builtin_module_names)
('_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').
Python Math Module
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 functions take a decimal number as argument and return an integer.
Example:
- #Importing math Module
- import math
- #Fractional number
- number=8.10
- #printing ceiling value of the fractional number
- print("Ceiling value of 8.10 is :",math.ceil(number))
- #printing floor value of the fractional number
- print("Floor value of 8.10 is :",math.floor(number))
Output:![]()
fabs function
fabs function is used to compute the absolute value of any number. If a number contains any negative(-) sign, then it removes that sign and returns a positive fractional number.
Example:Output:
- #Importing math Module
- import math
- number = -8.10
- #printing absolute value of the number
- print(math.fabs(number))
![]()
factorial function
This function takes a positive integer number and prints factorial of that number.
Example:
- #Importing math Module
- import math
- number = -8.10
- #printing absolute value of the number
- print(math.fabs(number))
Output:
Note: When you will enter negative number, then you will get a Value Error.Example:Output:
- #Importing math Module
- import math
- number = -5
- #printing factorial of the number
- print("fatorial of the number is",math.factorial(number))
![]()
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:Output:
- #Importing math Module
- import math
- print(math.fmod(5,2))
- print(math.fmod(-5,2))
- print(math.fmod(-5.2,2))
- print(math.fmod(5.2,2))
![]()
frexp function
This function returns mantissa and exponent in the pair(m,n) of any number x by solving the following equation.
Example:
Output:
- #Importing math Module
- import math
- print(math.frexp(24.8))

fsum function
It calculates accurate floating-point sum of values in iterable and calculates the sum of the list of data or range of data.
Example:Output:
- #Importing math Module
- import math
- #sum of list of data
- numbers=[.1,.2,.3,.4,.5,.6,.7,.8,8.9]
- print("sum of ",numbers," is :",math.fsum(numbers))
- #sum of range
- print("sum of 1 to 10 numbers is :",math.fsum(range(1,11)))
![]()
Power and Logarithmic Function
exp function
This function takes one parameter as a fractional number and returns e^x.
Example:
- #Importing math Module
- 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:Output:
- #Importing math Module
- import math
- print(math.exp(5)-1)
- print(math.expm1(5))
![]()
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:Output:
- #Importing math Module
- import math
- # log with Default base e
- print(math.log(2))
- # log with specific base
- print(math.log(64,2))
![]()
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:
- #Importing math Module
- import math
- print(math.log1p(2))
Output:![]()
log10 function
This calculates log with base 10.
Example:Output:
- #Importing math Module
- import math
- print(math.log10(1000))
![]()
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:
- #Importing math Module
- 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:Output:
- #Importing math Module
- import math
- print(math.sqrt(256))
![]()
Trigonometric Functions
In python we have the following trigonometric functions.
- sin: Takes one parameter in radian and return the sine of that radian.
- cos: Takes one parameter in radian and return the 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 the 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:
- #Importing math Module
- import math
- #sin function
- print("sin PI/2 :",math.sin(math.pi/2))
- #cos function
- print("cos 0 :",math.cos(0))
- #tan function
- print("tan PI/4 :",math.tan(math.pi/4))
- print()
- #asin finction
- print("asin 0 :",math.acos(0))
- #acos function
- print("acos 1 :",math.acos(1))
- #atan function
- print("atan 0.5 :",math.atan(0.5))
- print()
- #sinh function
- print("sinh 1 :",math.sinh(1))
- #cosh function
- print("cosh 0 :",math.cos(0))
- #tanh function
- print("tanh 1 :",math.tan(1))
- print()
- #asinh finction
- print("asinh 1 :",math.acosh(1))
- #acosh function
- print("acosh 1 :",math.acosh(1))
- #atanh function
- print("atanh 0.5 :",math.atanh(0.5))
- degrees: Converts radian to degrees.
- radians: Converts degree to radians.
Example:
- #Importing math Module
- import math
- print(math.degrees(1.57))
- print(math.radians(90))
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
- #Importing math Module
- import math
- #printing value of PI
- print("value of PI is",math.pi)
- #printing value of e
- print("value of e is",math.e)


Santhakumar MunuswamyPosted Oct 18, 2015, 4:05 AM
Good one
Harshad PansuriyaPosted Oct 10, 2015, 8:22 AM
Nice one
Nilesh JadavPosted Oct 10, 2015, 7:51 AM
Nice work sir