Python Modules notes
Python Modules notes
Python Library
Python library are also known as python package. It is collection of python model which are
further divided into sub module.
Example :
import math
print( dir(math) )
math Module
It contains different types of mathematical functions.
Commonly used functions in math module.
1. pi - gives the value of pi (3.14159)
2. e - gives the value of e (2.718281)
3. ceil( x ) – returns the smallest integer that is greater than or equal to x
>>> math.ceil(3.4)
Output 4
4. floor(x) – returns the largest integer that is less than or equal to x
>>> math.floor(100.12 )
Output 100
>>> math.floor( - 45.12)
Output -56
5. pow(x, y) – returns the (y power of x)
6. sqrt(x) – returns the squarroot of x
7. fabs(x) – returns the absolute value of x
>>> math.fabs(500.23)
Output 500
>>> math.fabs(-200)
Output 200
>>> math.fabs(-15.33)
Output 15.22
8. log10(x) – returns the base 10 logarithm of x
9. sin(x) – returns the sine of x
10. cos(x) – returns the cosine of x
11. tan(x) – returns the tangent of x
random Module
random module in python helps to generate random numbers. This module contains functions
that are used for generating random numbers.
Commonly used functions in random module.
1. randrange( ) - it generates an integer between its lower and upper argument. By
default, the lower argument is 0.
Example:
>>> random.randrange(30) # it will generate random number between 0 to 29
2. random( ) – it generates a random number from 0 to 1 such as 0.56. It can be used
to generate floating point values.
3. randint( ) – This function generates a random integer number between two given
numbers.
>>> random.randint(5, 30) # it will generate random number between 5 to 30
statistics Module
It helps to implement many common statistical formulas for efficient calculations.
Commonly used functions in statistics module.
1. mean( ) – calculates the arithmetic mean of the numbers in a list (average).
Example:
>>> statistics.mean([4,6,7,8,10,45])
Output 13.3333
2. median( ) – returns the middle value of numeric data in a list. This function find
the centre value, and if the data set has an even number of values, it averages the
two middle items.
Example:
>>> statistics.median( [1,2,3,8,9])
Output 3
>>> statistics.median([1,2,3,7,8,9])
Output 5.0