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

Chapter 9 - Modules in Python

Uploaded by

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

Chapter 9 - Modules in Python

Uploaded by

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

NAVODAYA VIDYALAYA SAMITI

HYDERABAD REGION

E-CONTENT
COMPUTER SCIENCE CLASS XI
COMPUTATIONAL THINKING AND
PROGRAMMING

Introduction to Python Modules


SHIFA JAMES ,
PGT CS, JNV KADAPA
Modules in Python - Contents

Introduction to Python modules


Importing math module
(pi, e, sqrt, ceil, floor, pow, fabs, sin, cos, tan);
Importing random module
(random, randint, randrange),
Importing statistics module
(mean, median, mode).
Learning Objectives

After completing this lesson, the students


should be able to:
Understand the need of modules in Python.
Understand the use of import keyword
Use import statement to include various modules to the
program
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

Prototype
Sl.
Function (General Description Example
No
form)
square root of x >>> math.sqrt(144)
x may be a positive 12.0
1 sqrt math.sqrt(x)
integer or floating point >>>>>> math.sqrt(.64)
0.8
number
>>> math.pow(3,2)
9.0
xʸ (x raised to the power y) >>> math.pow(4,2.5)
32.0
2 pow math.pow(x,y) x, y may be an integer or >>> math.pow(6.5,2)
floating point number 42.25
>>> math.pow(5.5,3.2)
233.97

>>> math.ceil(-9.7)
ceil ceiling value of x -9
>>> math.ceil (9.7)
3 math.ceil(x) x may be an integer or 10
floating point number >>> math.ceil(9)
9
Functions in math module
Prototype (General
Sl. No Function Description Example
form)

>>> math.floor(-4.5)
floor value of x -5
>>> math.floor(4.5)
4 floor math.floor(x) x may be an integer or 4
floating point number >>> math.floor(4)
4
sine of x in radians >>> math.sin(0)
x may be an integer or 0
5 sin math.sin(x)
floating point number in >>> math.sin(6)
-0.279
radians
cosine of x in radians >>> math.cos(0)
x may be an integer or 1
6 cos math.cos(x) floating point number in >>> math.cos(60)
-0.95241
radians
Function/ Prototype
Sl. No Description Example
constant (General form)

Tangent of x in radians >>> math.tan(0)


0
7 tan math.tan(x) x may be an integer or floating >>> math.tan(90)
point number in radians -1.9952

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

>>> math.pi
9 pi math.pi Return the constant value of pi 3.141592653……
(available precision)

>>> math.e
10 e math.e Return the constant value of 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

Prototype (General
Sl. No Function Description Example
form)

Random Real Number >>> random.random()


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

Random integer between x and


random.randrange(x,y) y where x and y are positive >>> random.randrange(3,7)
integers signifying 5
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

Prototype (General
Sl. No Function Description Example
form)

Arithmetic mean >>> statistics.


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

>>>statistics.
median Median (middle value) of x median([11,24,32,45,51])
2 statistics.median(x)
where x is a numeric sequence
32

>>> statistics.
mode([11,24,11,45,11])
Mode (the most repeated
11
3 mode statistics.mode(x) value) in x where x is a
>>> statistics.
sequence
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
Let us Summarise

Python modules
Importing modules
Importing math module
(pi, e, sqrt, ceil, floor, pow, fabs, sin, cos, tan);
Importing random module
(random, randint, randrange),
Importing statistics module
(mean, median, mode).
BIBLIOGRAPHY:

1. Computer Science Textbook for class


XI
by NCERT
2. Computer Science with Python
by Sumitha Arora
3. Computer Science with Python
by Preeti Arora

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy