Python modules-XI
Python modules-XI
• The modularity is one of the important feature of programing. It allows to divide the code
into different small units. The advantages of modularity are as following:
The ceil() function approximates the given number to the smallest integer,
greater than or equal to the given floating point number.
The floor() function returns the largest integer less than or equal to the given
number.
>>> import math
>>>math.ceil(4.5867)
5
>>>math.floor(4.5687)
4
math.pow()
The math.pow() method receives two float arguments, raises the first to the
second and returns the result. In other words, pow(2,3) is equivalent to 2**3.
>>>math.pow(2,4)
16.0
math module
math.fabs()
Returns the absolute value of x
>>> import math
>>> math.fabs(-5.5)
5.5
random.random()
• Genereate random number from 0.01 to 1.If we want a larger number, we can
multiply it.
>>>import random
>>>print(random.random() *100)
Random Module
randrange()
generate random numbers from a specified range and also allowing rooms for steps
to be included.
This function accepts the start, stop, and step values to generate random number by a
specified series. The syntax is as following:
>>>import math
>>>print(random.randrange(6, 9,2))
statistics module
statistics module
This module provides functions for calculating mathematical statistics of numeric
(Real-valued) data.
statistics.mean(data)
Return the sample arithmetic mean of data which can be a sequence or
iterator.The arithmetic mean is the sum of the data divided by the number of data
points(AVERAGE).
>>>import statistics
>>>print(statistics.mean([5,3,2]))
OUTPUT
3.3333333333333335
statistics.median(data)
Return the median (middle value) of numeric data. Median is the middle value of a
given dataset.
>>>import statistics
>>>print(statistics.median([5,5,4,4,3,3,,2]))
OUTPUT
3.5
statistics module
statistics.mode(data)
Return the most common data point from discrete or nominal data. The mode
(when it exists) is the most typical value, and is a robust measure of central
location.If data is empty, or if there is not exactly one most common value,
StatisticsError is raised.
>>>import statistics
>>>print(statistics.mode([1, 1, 2, 3, 3, 3, 3, 4]))
OUTPUT
3