0% found this document useful (0 votes)
3 views5 pages

Python Modules

The document provides an overview of Python modules, explaining their purpose for code organization and reusability. It details built-in modules like math, random, and statistics, along with examples of their functions and usage. Additionally, it includes multiple-choice questions and code fragments to illustrate practical applications of these modules.

Uploaded by

Eshwara R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Python Modules

The document provides an overview of Python modules, explaining their purpose for code organization and reusability. It details built-in modules like math, random, and statistics, along with examples of their functions and usage. Additionally, it includes multiple-choice questions and code fragments to illustrate practical applications of these modules.

Uploaded by

Eshwara R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Modules

Python Modules:

Modules are the files in python used for grouping similar codes, to get an easy access to those codes.
Python Modules facilitates reusability and easy categorization of codes.
Python Built In Modules:
Python facilitates its users a variety of modules already defined in Python library that makes Python a easy
to use programming language. Some of these modules are:
Importing a Python Module:
Python modules can be imported in three ways, using:

Python import statement:


To use the functionality present in any module, you have to import it into your current program. You need to
use the import keyword along with the desired module name.
Python “import” statement is used to import a Python module. For Example
import math

Python from.. import statement:


Python also facilitates to import a specific attribute from a Python module. This can be done using Python
“from..import” statement. For example
from math import sqrt # It will import only sqrt method from math module

Python from.. import* statement:


Python “from..import*” statement is used to import a complete Python module. For Example
from math import * # it imports entire module
Python Math Module:
Python math module contains different built in mathematical functions and mathematical constants.
Math Functions:

FUNCTION USES Example


ceil () The ceil() function returns the smallest integer not [Link](1.03) gives 2.0
less than num [Link](-1.03) gives -1.0
floor() The floor() function returns the largest integer not [Link](1.03) gives 1.0
greater than num [Link](-1.03) gives -2.0
sqrt() The sqrt() function returns the square root of num. If [Link](81.0) gives 9.0
num < 0 , domain error occurs.
pow() The pow() function returns the base raised to exp [Link](5.0,0) gives 1
power [Link](3.0,4) gives 81
fabs() The fabs() returns the absolute value of num [Link](2.0) gives 2.0
[Link](-2.0) gives 2.0

90
sin() The sin() function returns the sine of arg. The value [Link](val)
of arg must be in radians. (val is a number)
cos() The cos() function returns the cosine of arg. The [Link](val)
value of arg must be in radians. (val is a number)
tan() The tan() function returns the tangent of arg. The [Link](val)
value of arg must be in radians. (val is a number)

The math module of Python also makes available two useful constants namely pi and e. Which we can use as
[Link] gives the mathematical constant π = 3.141592…, to available precision.
math.e gives the mathematical constant e = 2.718281…, to available precision.

Random Module
Function Uses Example
random() Used to generate a random floating – point number Import random()
between 0.0 to 1.0 that is, including zero but excluding print([Link]())
one. 0.022353
randint() randint(start, stop) is used to generate a random [Link](10,15)
number between start and stop where both the it may generate any one of the
numbers are inclusive. values given below
10,11,12,13,14,15
randrange() • [Link](<stopvalue>) is used to • [Link](35)
generate a random number in the range 0 to It will generate a random
<stopvalue> number from 0 to 35
• [Link](start,stop) is used to • [Link](11,45)
generate a random number in the range start to It will generate a random
stop number in the range 11 ..
• [Link](start,stop,step) is used to 45
generate a random number in the range start to • [Link](11,45,
stop, but here, the difference between two such 4)
generated random numbers will be a multiple of it may generate any one
step value. of the values given
below11,15,19,23,27,31,3
5,39,43

91
Statistics Module
mean() This method calculates the mean (average) of the givendata import statistics
set. It add up all the given values, then divide by number of seq =
values in the set. [5,6,7,5,6,5,5,9,11,12,23,5]
[Link](seq)
it gives 8.25

median() This method calculates the median (middle value) of the import statistics
given data set. seq =
[5,6,7,5,6,5,5,9,11,12,23,5]
[Link](seq)
it gives 6.0
mode() This method determines the central tendency of numerical import statistics
or nominal data. It is used to find the most frequent number seq =
in a sequence. [5,6,7,5,6,5,5,9,11,12,23,5]
[Link](seq)
it gives 5

MCQs

1. To include the use of functions which are present in the random library, we must use the option:
a) import random b) random.h c) [Link] d) [Link]
2. The output of the following Python code is either 1 or 2.
import random
[Link] (1 , 2)
a) True b) False

3. What will be the output of the following Python function if the random module has already been
imported?
[Link](3.5 ,7)
a) b) Any integer between 3.5 and 7, including 7
b) Any integer between 3.5 and 7, excluding 7
c) The integer closest to the mean of 3.5 and 7
d) Error

4. What will be the output of the following Python code?


[Link](0,91,5)
a) 18 b) 10 c) 79 d) 95

5. Which of the following is not a built in module in Python?


a) math b) os c) pi d) random

6. What will the following code result as?


import math
x = 100

92
print(x > 0 and [Link](x))
a) True b) 1 c) 10 d) 10.0

7. What will the following code result as?


import statistics
seq = [10,20,20,30,12,10]
print([Link]())
a) 30 b) 10 c) 17 d) 1

8. What may be the possible values printed by following statements?


import random
for I in range(3):
print([Link](10,18))
a) 13 12 10 b) 11 18 19 c) 10 9 18 d) 10 9 8

Answers:
1 2 3 4 5 6 7 8
A A d b c d c a

Q1) What is random module in Python?


Ans: Python Random module is an in-built module of Python that is used to generate random numbers in
Python.
Q2) Write a code fragment to generate a random floating number between 45.0 and 95.0. Print this number
along with its nearest integer greater than it.
Ans:
import random
import math
fnum = [Link]() * (95 - 45) + 45
inum = [Link](fnum)
print("Random Numbers between 45...95: ")
print(fnum)
print("Nearest higher integer : ", inum)
Output
Random Numbers between 45...95:
56.31601558476624
Nearest higher integer : 57

Q2) Write a program to generate two random integers between 500 and 760. Print the average of these
number along with these numbers.
Ans:
import random
num1 = [Link](500,760) - 500
num2 = [Link](500,760) - 500
avg = (num1 + num2)/2
print("Number 1 : ", num1)
print("Number 2 : ", num2)

93
print("Average : ", avg)

Output
Number 1 : 147
Number 2 : 153
Average : 150.0

Q2) Given a list containing these values [22,13,28,13,22,25,7,13,25] . Write code to calculate mean,
median and mode of this list?
Ans:
import statistics as s
L = [22,13,28,13,22,25,7,13,25]
LMean = [Link](L)
LMedian = [Link](L)
LMode = [Link](L)
print("List ....... ")
print(L)
print("Mean : ", LMean)
print("Median : ", LMedian)
print("Mode : ", LMode)
Output
List........
[22, 13, 28, 13, 22, 25, 7, 13, 25]
Mean : 18.666666666666668
Median : 22
Mode : 13

94

You might also like