0% found this document useful (0 votes)
2 views4 pages

Ch-8 Introduction to Python Modules

The document provides an introduction to Python modules, explaining that a module is a file containing related variables, class definitions, and functions. It details how to import modules and access their objects, distinguishing between built-in and user-defined modules. Additionally, it covers specific built-in modules like Math, Statistics, and Random, along with examples of their functions and usage.

Uploaded by

hrito763
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)
2 views4 pages

Ch-8 Introduction to Python Modules

The document provides an introduction to Python modules, explaining that a module is a file containing related variables, class definitions, and functions. It details how to import modules and access their objects, distinguishing between built-in and user-defined modules. Additionally, it covers specific built-in modules like Math, Statistics, and Random, along with examples of their functions and usage.

Uploaded by

hrito763
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
You are on page 1/ 4

COMPUTER SCIENCE- XI BY SUMIT GARG SIR

Ch- 8 Introduction to Python Modules

Modules

A python module is a file (.py) containing variables, class definitions, statements and functions
related to a particular task. Modules are used to access prewritten code to perform tasks with ease.
A library refers to a collection of modules that together cater to a specific types of needs or
applications.

Importing Modules

To use definitions inside a module, we can import it by using import keyword.


The import statement can be used in two forms:
• To import entire module: import <module>
• To import selected objects from module: from <module> import <object>

Accessing objects defined in modules

To access definitions of a module, we have to write


<module> . <object>
Or
<module> . <function-name> ( )

Types of Python Modules


There are two types of Python modules:
1. Built-in Modules
2. User defined Modules
1. Built-in Modules: There are many built in modules in Python Library. Some of the common built-in
modules are:
• Math module
• Statistics module
• Random module
2. User defined Modules: Modules that are created by python programmers.

Math Module
It provides math related functions and objects. It comes under standard library of Python.

floor(n): This function returns the largest integer which is less than or equal to n. for example
import math
print(math.floor(23.45)
Output : 23
1
COMPUTER SCIENCE- XI BY SUMIT GARG SIR
ceil(n): This function returns the smallest integer which is more than or equal to n. for example
import math
print(math.ceil(23.45)
Output : 24

pow(m, n): This function returns m raise to the power n. for example
import math
print(math.pow(2,3))
Output : 8

sqrt( ): This function return the square root of a number passed as argument. for example
import math
print(math.sqrt(16))
Output : 4.0

fabs( ): This method return the absolute value(positive value) of a number passed as an argument.
for example
import math
print(math.fabs(-9))
Output : 9.0

sin( ): This function returns the sine of value passed as argument. for example
import math
print(math.sin(90))
Output : 0.8939966636005579
NOTE : Value passed in this function should be in radian

cos( ) : This function returns the cosine of value passed as argument. for example
import math
print(math.cos(90))
Output : -0.4480736161291701
NOTE : Value passed in this function should be in radian.

tan( ): This function returns the tangent of value passed as argument. for example
import math
print(math.tan(90))
Output : -1.995200412208242

NOTE : Value passed in this function should be in radian.


2
COMPUTER SCIENCE- XI BY SUMIT GARG SIR
pi: It returns mathematical constant. for example
import math
print(math.pi)
Output : 3.141592653589793

e: It returns the mathematical constant


import math
print(math.e)
Output : 2.718281828459045

Statistics Module :Provides functions for statistical calculation on a list of values. Functions of
statistics module are discussed below:
median( ): This function returns the median of the data. Median means middle value. for example :
import statistics
print(statistics.median([23, 45, 56, 67, 78]))
Output : 56

mean( ): This function returns the average of the data passed as an argument. for example
import statistics
print(statistics.mean([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
Output : 5.5

mode( ): This function returns the mode ie a number with maximum number of occurrence. for
example
import statistics
print(statistics.mean([1, 2, 1, 4, 1, 2, 3, 2, 1, 5]))
Output : 1

Random Module :Provides functions to generate random numbers. Functions of random module
are discussed below:
randrange( ): This function generates a random number between the lower and upper limit. For
example: to generate a random number between 1 to 20:
import random
a = random.randrange(10, 20)
print(a)

Note: Default lower limit is zero. Randrange( ) always generates a number one less than the higher
limit.
3
COMPUTER SCIENCE- XI BY SUMIT GARG SIR
random( ): This function generated a random floating point from 0 to 1 (including 0 excluding 1).
For example:
import random
a = random.random( )
print(a)

randint( ): This function generates a random integer between the lower and upper limit (including
both the limits). For example:
import random
a = random.randint(20, 30)
print(a)

You might also like