0% found this document useful (0 votes)
2 views

Introduction to Python Moodules

Uploaded by

n73928957
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to Python Moodules

Uploaded by

n73928957
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Modules in Python

A module is simply a python file where statements,

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.

We can use the module we created, by using the import statement.

How to import modules in Python?

Python module can be accessed in any of following ways.

1. Python import statement


import math
print(“2 to the power 3 is ", math.pow(2,3))
Just similar to math ,user defined module can be accessed using import statement
2. Import with renaming
import math as mt
print(“2 to the power 3 is ", mt.pow(2,3))
3. Python from...import statement
from math import pow
print(“2 to the power 3 is ", pow(2,3))
4. Import all names
from math import *
print(“2 to the power 3 is ", pow(2,3))

Functions of math module:


To work with the functions of the math module, we must import the math module
in the program.
import math

S. No. Function Description Example

Returns the square root of >>>math.sqrt(49)


1 sqrt( ) a number 7.0

>>>math.ceil(81.3)
2 ceil( ) Returns the upper integer 82

>>>math.floor(81.3)
3 floor( ) Returns the lower integer 81

Calculate the power >>>math.pow(2,3)


4 pow( ) of a number 8.0

5 fabs( ) Returns the absolute value of a >>>math.fabs(-5.6)


number 5.6

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

Functions of random module:

To work with the functions of a random module, we must import a random module in the
program.

import random

S. No. Function Description Example

1 random () It returns a random float >>>random.random()


x, such that 0 ≤ x<1 ><1
0.281954791393

2 randint(a, b) It returns a int x between >>>random.randint(1,10)


a & b such that a ≤ x ≤ b
5

3 Randrange ([start,] It returns a random item >>>random.randrange(100,1000,3)


stop [,step]) from the given range upto 150
stop-1.

Functions of statistics module:

To work with the functions of the statistics module, we must import the statistics module in the
program.

import statistics

S. No. Function Description Example

1 mean ( ) It returns mean of a list of numbers >>> print(statistics.mean([1, 3, 5, 7, 9,


11]))
6

2 median() It returns the median (middle value) of >>> print(statistics.median([1, 3, 5, 7,


the given data set 9, 11, 13]))
7

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=”# “)

(i) 10#40#70# (ii) 30#40#50# (iii)50#60#70# (iv) 40#50#70#


4
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 START and END. 2

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],"#"

(i) 20#50#30# (ii) 20#40#45# (iii) 50#20#40# (iv) 30#50#20#

Very Short Answers

1 Predict the output:


from math import *
x=12
y=10 * sqrt(pow(x,2))
2 Identify the errors and correct them:
Import math
Y=36
S=sqrt(Y)
Print(S)
3 Predict the output:
import math
num=14.14
print(math.ceil(num))
print(math.floor(num))
4 Predict the output: (What are the maximum and minimum possible values?)
import random
print(random.random(5))
5 Predict the output:
li=[1,2,3,4,5,6,7,8,9,10]
from statistics import *
print(mean(li))
print(median(li))
6 Identify the errors and correct them:
li=[1,2,3,4,5,6,7,8,9,10]
from statistics import *
print(li.mean())
print(statistics.median(li))
7 Predict the output: (What are the possible values?)
import random
print(random.randint(5,10))
8 Predict the output: (What are the possible values?)
import random
print(random.randrange(10,20,2))
9 Write program to find square root of a function using math module
10 Predict the output:
li=[1,2,3,4,4,4,4,4,4,5,5,5,5,5,6,7,8,8,8,8,9,10]
from statistics import *
print(mode(li))

74

You might also like