Introduction to Python Moodules
Introduction to Python Moodules
classes, Objects, functions, constants and variables are defined. The file name is the module name
with .py extension. Definitions from a module can be imported into other modules. It is a kind of code
library.
>>>math.ceil(81.3)
2 ceil( ) Returns the upper integer 82
>>>math.floor(81.3)
3 floor( ) Returns the lower integer 81
70
>>>math.sin(90)
6 sin( ) Returns the sine of a number 0.89399
>>>math.cos(90)
7 cos() Returns the cosine of a number -0.448073
>>>math.tan(90)
8 tan() Returns the tangent of a number
-1.995200
To work with the functions of a random module, we must import a random module in the
program.
import random
To work with the functions of the statistics module, we must import the statistics module in the
program.
import statistics
71
3 mode() It returns the mode (central tendency) >>> print(statistics.mode([1, 3, 3, 3, 5,
of the given numeric or nominal data 7, 7, 9]))
set. 3
MINDMAP
MCQ
1
Study the following program and select the possible output(s) from the options (i) to (iv)
following it. Also, write the maximum and the minimum values that can be assigned to the
variable Y.
import random
X=random.random()
Y=random.randint(1,4)
print(int(X),”:”,Y+int(X))
i) 0 : 0 ii) 1 : 6 iii) 2 : 4 iv) 0 : 3
2 Observe the following program and answer the questions that follow:
import random
X=3
N=random.randint(1,X)
for i in range(N):
print (i,”#”,i+1)
72
a) What is the minimum and maximum number of times the loop will execute?
b) Find out, which line of output(s) out of (i) to (iv) will not be expected from the
program?
(i) 0#1 (ii) 1#2 (iii) 2#3 (iv) 3#4
3
What possible outputs(s) are expected to be displayed on screen at the time of execution of
the program from the following code? Also specify the maximum values that can be
assigned to each of the variables FROM and TO.
import random
AR=[20,30,40,50,60,70];
FROM=random.randint(1,3)
TO=random.randint(2,4)
for K in range(FROM,TO+1):
print (AR[K],end=”# “)
import random
SCORE=[20,40,10,30,15];
START=random.randint(1,3)
END=random.randint(2,4)
for I in range(START,END+1):
print(SCORE[I],"&")
(i) 10&40&20& (ii) 10&30&15& (iii) 40&10&30& (iv) 20&40&10&
5 What are the possible outcome(s) executed from the following code? Also specify the
maximum and minimum values that can be assigned to variable N.
import random
PLAY=[40,50,10,20]"EAST","WEST","NORTH","SOUTH";
ROUND=random.randint(2,3)
for J in range(ROUND,1,–1):
print PLAY[J],”:”
(i) 20:10: (ii) 20:10:50: (iii) 20: (iv) 40:50:20:
6
What possible output(s) are expected to be displayed on screen at the time of execution of
the program from the following code ? Also specify the maximum values that can be
assigned to each of the variables BEGIN and LAST. 2
import random
73
POINTS=[20,40,10,30,15]
POINTS=[30,50,20,40,45]
BEGIN=random.randint(1,3)
LAST=random.randint(2,4)
for C in range(BEGIN,LAST+1):
print POINTS[C],"#"
74