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

Fnctions and Liberaries

The document explains the concept of modules in Python, highlighting their definition, advantages, and structure. It details how to import modules and provides examples of built-in modules like math, random, and statistics, along with their functions. Additionally, it covers points to note when using modules and demonstrates importing specific functions using the 'from' statement.

Uploaded by

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

Fnctions and Liberaries

The document explains the concept of modules in Python, highlighting their definition, advantages, and structure. It details how to import modules and provides examples of built-in modules like math, random, and statistics, along with their functions. Additionally, it covers points to note when using modules and demonstrates importing specific functions using the 'from' statement.

Uploaded by

ym7572973
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

MODULES IN PYTHON

Introduction
• A book is generally divided into chapters. Why?

• If it is not divided into chapters, reading will be boring and problematic.

• Similarly, a big program is generally divided into small units, which are
known as modules.

• The process of dividing a big program into small modules is known


as modularity.

• A module is itself an individual unit.


Advantages

• Modularity decreases complexity of a


program.

• Modularity splits the lengthy program into


small and manageable subprograms and
set documented boundary between each.
• Modularity also enhance the capability of
Reusability .
Definition
▪A module is a python file (.py) containing
a collection of function definitions ,
variables and constants.

▪To use a module, we need to import the module.


▪For that , we use the keyword import
Structure of Python Module

• A Python Module contains several things other than functions.

• A Python file (.py file) can contain following objects-


❖ Docstring : Triple Quoted comments which are important for
documentation.
❖ Variables and constants : labels for data .
❖ Classes : Blue print for objects.
❖ Objects : Instances of classes.
❖ Statements : Instructions
❖ Functions :Group of instructions
Python Module -Structure

docString starts and ends with triple quotes


“”””.

This module has 2 functions and 2 docstrings.|

Importing module.

Using object of module.


Importing modules to Python
Program
▪To use a module, we need to import the module. Once we import a module,
we can directly use all the functions of that module. The syntax of import
statement is as follows:
▪ import modulename1 [,modulename2, …]
Eg : import math
▪This gives us access to all the functions in the module(s). To call a function of
a module, the function name should be preceded with the name of the module
with a dot(.) as a separator. The syntax is as shown below:
▪ modulename.functionname()

▪ Eg : print(math.sqrt(144))
Built-in Modules
▪Python library has many built-in modules that are easy to use for
the programmers. Some commonly used modules and the
frequently used functions that are found in those modules are
listed below:
⮚ math
⮚ random
⮚ statistics
Math Module

▪It contains different types of mathematical functions.

▪Most of the functions in this module return a float


value.
▪In order to use the math module we need to import it
using the following statement:
import math
Functions in math module
Sl. No Function Prototype Description Example
(General form)

1 sqrt math.sqrt(x) square root of x >>> math.sqrt(144)


x may be a positive integer or 12.0
floating point number >>>>>> math.sqrt(.64)
0.8

2 pow math.pow(x,y) xy (x raised to the power y) >>> math.pow(3,2)


9.0
x, y may be an integer or >>> math.pow(4,2.5)
floating point number 32.0
>>> math.pow(6.5,2)
42.25
>>> math.pow(5.5,3.2)
233.97

3 ceil math.ceil(x) ceiling value of x >>> math.ceil(-9.7)


-9
x may be an integer or floating >>> math.ceil (9.7)
point number 10
>>> math.ceil(9)
9
Functions in math module
Sl. No Functio Prototype Description Example
n (General form)
4 floor math.floor(x) floor value of x >>> math.floor(-4.5)
x may be an integer or floating -5
point number >>> math.floor(4.5)
4
>>> math.floor(4)
4

5 sin math.sin(x) sine of x in radians >>> math.sin(0)


x may be an integer or floating 0
point number in radians >>> math.sin(6)
-0.279

6 cos math.cos(x) cosine of x in radians >>> math.cos(0)


x may be an integer or floating 1
point number in radians >>> math.cos(60)
-0.95241
Sl. No Function Prototype Description Example
/ (General
constant form)
7 tan math.tan(x) Tangent of x in radians >>> math.tan(0)
x may be an integer or floating point 0
number in radians >>> math.tan(90)
-1.9952

8 fabs math.fabs(x) absolute value of x >>> math.fabs(6.7)


x may be an integer or 6.7
floating point number >>> math.fabs(-6.7)
6.7
>>> math.fabs(-4)
4.0

9 pi math.pi Return the constant value of pi >>> math.pi


3.141592653……
(available precision)

10 e math.e Return the constant value of e >>> math.e


2.7182818……
(available precision)
Random Module

▪This module contains functions that are used for generating


random numbers.
▪For using this module, we can import it using the following
statement:
import random
Functions in random module
Sl. No Function Prototype Description Example
(General form)

1 random random.random() Random Real Number >>> random.random()


(float) in the range 0.0 to 1.0 0.65333522
2 randint random.randint(x,y) Random integer between x and >>> random.randint(3,7)
y where x, y are integers such 4
that >>> random.randint(-3,5)
x <= y 1
>>> random.randint(-5,-3)
-5.0
3 randrange random.randrange(y) Random integer between 0 and >>> random.randrange(5)
y where y is a positive integer 4
signifying the end value

random.randrange(x,y) Random integer between x and >>> random.randrange(3,7)


y where x and y are positive 5
integers signifying
the start and end value
Statistics Module

▪This module provides functions for calculating statistics of numeric


(Real-valued) data.
▪It can be included in the program by using the following statements:
import statistics
Functions in statistics module

Sl. No Function Prototype Description Example


(General form)

1 mean statistics.mean(x) Arithmetic mean >>> statistics.


Where x is a numeric mean([11,24,32,45,51])
sequence
32.6

2 median statistics.median(x) Median (middle value) of x >>>statistics.


where x is a numeric sequence median([11,24,32,45,51])

32

3 mode statistics.mode(x) Mode (the most repeated >>> statistics.


value) in x where x is a mode([11,24,11,45,11])
sequence 11
>>> statistics.
mode(("red","blue","red"))
'red'
Points to be noted while using a module

1. import statement can be written anywhere in the program


2. A Module must be imported only once in a program
3. In order to get a list of modules available in Python, we can use the
following statement:
>>> help("module")
Example
>>> help("math")
Importing modules using from statement

Importing module can be done using from statement so that


only specific functions / attributes can be included in other programs.
Syntax :
▪ from modulename import functionname [, functionname, ...]
Example:
from math import sqrt
Example 1.
>>> from random import random
>>> random() #Function called without the module name
Output:
0.9796352504608387

Example 2.
>>> from math import ceil, sqrt
>>> value = ceil(624.7) #Function called without the module name
>>> sqrt(value)
Output:
25.0

You might also like