COMPUTER SCIENCE WITH PYTHON [PYHTON MODULES]
INTRODUCTION TO PYTHON MODULES
A module is a piece of Python code. A module allows you to logically organize your
Python code. A module is a file that contains Python statements and definitions. The
modules in Python have the .py extension.
Python itself is having a vast module library and packages with the default installation.
To use any package in your code, you must first make it accessible. You have to import
it using the import [Link] example,
import math
import random
import statistics
Notice that after using the above three import statements, you can access all built-in
functions into yourcurrent Python program or at interactive mode.
DIFFERENT IMPLEMENTATIONS OF IMPORT STATEMENT
Remember that to use a math module, you must use the dot(.) notation with the
module and method name except abs() function.
1. import math
For example,
>>> import math
>>> print ([Link](100)) # prints: 10.0
2. from math import sqrt, pi
The above statement imports two methods sqrt and the value of a constant pi into
the current application.
For example,
>>> from math import sqrt, pi
>>> print (sqrt(100), pi) # prints: 10.0 3.141592653589793
3. from math import *
The above statement imports all the methods from math module into the current application.
For example,
>>> from math import *
>>> print (sqrt(100), pi, floor(10.91)) # returns: 10.0 3.141592653589793 10
USING MATH MODULE
Python has a math module that provides most of the familiar mathematical functions.
The mathematical and trigonometric functions are prototyped in the math module. If
you print the module object, you get some information about it:
>>> import math
>>> print (math)
<module 'math' (built-in)>
To access one of the functions you have to specify the name of the module and the
name of the function separated by a dot (also known as a period). This format is called
dot (.) notation.
The following two examples produces the values of two mathematical constants pi and
e:
Page 1 of 5
COMPUTER SCIENCE WITH PYTHON [PYHTON MODULES]
>>> import math
>>> print ([Link])
3.141592653589793
>>> print (math.e)
2.718281828459045
Function Description
abs() This function returns the absolute value of a number. The
argument may be an integer or a floating point number. No need for
math module.
print (abs(–55)) # returns: 55
print (abs(200.12)) # returns: 200.12
ceil() This method returns ceiling value of x - the smallest integer not less
than x.
import math
print ([Link](–55.17)) # returns: –55
print ([Link]([Link])) # returns: 4
floor() This method returns floor of x – the largest integer not greater than
x.
import math
print ([Link](200.12)) # returns: 200
print ([Link]([Link])) # returns: 3
fabs() This method returns an absolute (positive) value of any numeric
expression.
import math
print ([Link](200.72)) # returns: 200.72
print ([Link]([Link])) # returns: 3.141592653589793
pow() This method returns the value of xy.
import math
print ([Link](2, 4)) # returns: 16.0
print ([Link](100, –2)) # returns: 0.0001
print ([Link](3, 0)) # returns: 1.0
sqrt() This method returns the square root of x for x > 0.
import math
print ([Link](144)) # returns: 12.0
print ([Link](7)) # returns: 2.6457513110645907
cos() This method returns the cosine of x radians.
import math
print ("cos(30) : ", [Link](30)) # returns: 0.15425144988758405
print ("cos(0) : ", [Link](0)) # returns: 1.0
Page 2 of 5
COMPUTER SCIENCE WITH PYTHON [PYHTON MODULES]
sin() This method returns the sine of x, in radians.
import math
print ("sin(30) : ", [Link](30)) # returns: –0.9880316240928618
print ("sin(0) : ", [Link](0)) # returns: 0.0
tan() This method returns the tangent of x radians.
import math
print ("tan(30) : ", [Link](30)) # returns: –
6.405331196646276
print ("tan(0) : ", [Link](0)) # returns: 0.0
USING RANDOM MODULE
Random numbers are heavily used in Computer Science for programs that involve
games or simulations. Python has a random module to generate random numbers.
>>> import random
>>> print (random)
<module 'random' from 'C:\\Python37\\lib\\[Link]'>
Function Description
random() This function generates a random number from 0 to 1 including
0, but excluding 1.
from random import random
random() # returns: 0.1916168207727299
random() # returns: 0.689060949995414
randrange() This method generates an integer between its lower and upper
argument. By default, the lower argument (including lower limit
and excluding upper limit) is 0.
import random
r_number = [Link] (50) # creates a random number
between 0 – 49
print (r_number) # returns: 13
r_number = [Link] (100, 1000)
print (r_number) # returns: 150
randint() This function accepts two parameters, a and b, as the lowest and
highest number; returns a number N in the inclusive range[a,b],
which signifies a<=N<=b, where the endpoints are included in the
range.
This function is best used in guessing number.
import random
print ([Link](0, 5)) # returns 4
Note. Remember that this function return any of the number
either 0, 1, 2, 3, 4 or 5.
Page 3 of 5
COMPUTER SCIENCE WITH PYTHON [PYHTON MODULES]
To generate a number between 0 and 9.
>>>import random
>>>print([Link](0,9))
7
USING STATISTICS MODULE
Python statistics module is used to calculate mathematical statistics of numeric (Real-
valued) data. To access Python's statistics functions, we need to import the statistics
module. To import all statistics module:
>>> import statistics
>>> print (statistics)
<module 'statistics' from 'C:\\Python37\\lib\\[Link]'>
Function Description
mean() This function is used to find the arithmetic mean of a set of data
(list, tuple, etc.) which is obtained by taking the sum of the data
and then dividing the sum by the total number of values in the set.
For example, if S = [8, 7, 10, 9, 6, 4, 11] is a list data-set, then
mean is:
8 + 7 + 10 + 9 + 6 + 4 + 11
𝑀𝑒𝑎𝑛 = ____________________________
7
55
𝑀𝑒𝑎𝑛 = ______
7
import statistics
S = [8, 7, 10, 9, 6, 4, 11] # S is a list
Smean = [Link](S)
print("Mean is :", Smean) # prints: Mean is : 7.857142857142857
Note. If a data set empty, then the StatisticsError will be raised.
median() The median of a set of data is the middlemost number in the set.
The median is also the number that is halfway into the set. In
Python, the median() function is used to calculate the median or
middle value of a given set of numbers. Let us see an orderly odd
data-set with 5 values:
5 10 15 20 25
import statistics
S = [5, 10, 15, 20, 25]
Page 4 of 5
COMPUTER SCIENCE WITH PYTHON [PYHTON MODULES]
Smedian = [Link](S)
print("Median is :", Smedian) # prints: Median is : 15
Let us see an orderly even data-set with 10 values:
4 5 10 11 15 17 18 20 22 25
Since there is an even number of items in the data set, we find we
find the middle pair of numbers and then find the value that is half
way between them, i.e., compute the median by taking the mean of
the two middlemost numbers, i.e., (15 + 17) / 2 = 16.
import statistics
X = [4, 5, 10, 11, 15, 17, 18, 20, 22, 25]
print ('Median of X =', [Link](X))
# prints: Median of X = 16.0
Note. If a data set empty, then the StatisticsError will be raised
mode() In statistics, the mode of a set of data is the value in the set that
occurs most often. The mode() in function Python is used to
calculate the mode of given continuous numeric or nominal data.
1 1 2 3 3 3 4
import statistics
X = [1, 1, 2, 3, 3, 3, 4]
print ('Mode of X =', [Link](X)) # prints: Mode of X = 3
Let us see another example with data-set of string values:
Name = ('Anmol', 'Kiran', 'Vimal', 'Sidharth', 'Riya', 'Vimal')
print([Link](Name)) # prints: Vimal
Note. If a data set contains two equally common values or an empty
data set, then the StatisticsError will be raised.
Page 5 of 5