[Go to site: main page, start]

0% found this document useful (0 votes)
16 views45 pages

Python Libraries and Modules Overview

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)
16 views45 pages

Python Libraries and Modules Overview

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

DAV SDPS PUBLIC SCHOOL,BHADRAK

STD-XII
COMPUTER SCIENCE
CH-4(USING PYTHON LIBRARIES)
PYTHON LIBRARIES
 A large and complex programs should be
divided into functions.
 As we write more and more functions in a

program, we should consider organizing the


functions by storing them in modules.
 Python program is made using 3 different

components :
 Library or Package

 Modules

 Functions or sub modules


RELATION BETWEEN MODULE,
PACKAGE AND LIBRARY
 Module: A module is a file containing python
definition, functions, variables, classes and
[Link] extension of this file is “.py”.

 Python Package: Is a directory or folder of


Python Modules.A package is a collection of
related modules that work together to
provide certain functionality.

 Library: Is a collection of many packages in


Python.
RELATION BETWEEN MODULE,
PACKAGE AND LIBRARY

LIBRARY

PACKAGE1 PACKAGE2

.py .py .py .py .py


PYTHON SCRIPT V/S MODULE
Script: A script is a Python file
that’s intended to be run directly.
When you run it it should do
something.
Module: A module is a Python file
that’s intended to be imported into
scripts or other [Link] often
defines members like classes,
functions and variables intended to
be used in other files that import it.
PYTHON SCRIPT V/S MODULE

def add(a,b):
return a+b def add(a,b):
return a+b
result=add(6,9)
print(result)
Python
Module
Python Script
NOTES:
Idle-file-open-copy the path-paste in the
quickaccess search box
Open lib folder – many python inbuilt modules will
be there ex-statistics(search n get it)ctrl+f –type
mean,mode etc.
Or can get other functions say in math module or
random module.
If not getting math module then in python prompt
type import math-help(math) then enter.
Python is open source so source code of the
modules are availble.
Folder within another folder –modules(only python
files)
Folder not within another folder –package
ADVANTAGES OF PYTHON MODULES
 Putting code in module is useful because of
the ability to import the module functionality.
 A module can be used in some other python

code.
Hence it provides the facility of code
reusability.
o A module allows us to logically organize our

Python code.
o Similar types of attributes can be placed in a

single module.
o It reduces the complexity of a program to a

large extent.
STRUCTURE OF PYTHON MODULE
 A python module is simply a normal python
file(.py) and contains functions, constants and
other elements.
 Python module may contains following objects:
docstring Triple quoted comments. Useful for documentation
purpose
Variables and For storing values
constants
Classes To create blueprint of any object
Objects Object is an instance of class. It represent class in
real world
Statements Instruction
Functions Group of statements
COMPOSITION/STRUCTURE OF
COMPOSITION/STRUCTURE
PYTHON MODULE OF PYTHON
MODULE
MODULE
S OTHER
VARIABLES
PYTHON
MODULE
FUNCTIONS S

VARIABLES
IMPORT
CLASSES
MEMBER
OTHER
SMETHODS PYTHON
MODULE
S
IMPORTING PYTHON MODULES
 To import entire module
 import
<module name>
 Example: import math

 To import specific function/object from


module:
 from<module_name> import <function_name>
 Example: from math import sqrt

 import * : can be used to import all


names from module into current calling
module
IMPORTING PYTHON MODULES
i)Using import statement:
 Import<module_name>

 import <Module1>,<Module2>………

<Modulen>

To access a particular method from any


module:
ModuleName,FunctionName()
IMPORTING PYTHON MODULES
 Using From statement: Used to access
specific attributes from a module.
from<module>import<functionname>
from<module>import<function1><function2>
…. <functionN>
 To access a particular method from any

module:
functionName()
a) from module import *
MODULE ALIASING
Renaming the module at
the time of import, using
as keyword.
Import<ModuleName>as<

aliasname>
Import math as m

[Link](144)
ACCESSING FUNCTION/CONSTANT OF
IMPORTED MODULE
 To use function/constant/variable of
imported module we have to specify
module name and function name
separated by dot(.). This format is
known as dot notation.
 <module_name>.<function_name>
 Example: print([Link](25))
 How ever if only particular function is
imported using from then module name
before function name is not required.
We will see examples with next slides.
TYPES OF MODULES
 There are various in-built module in
python, we will discuss few of them
🞑 Math module
🞑 Random module
🞑 Statistical module
MATH MODULE
 This module provides various function to
perform arithmetic operations.
 Example of functions in math modules
are: sqrt ceil floor pow
fabs sin cos tan
 Example of variables in math
modules are:
🞑 pi
🞑 e
MATH MODULE FUNCTIONS
 sqrt(x) : thisfunction returns the
square root
number( of module name
is required
x). before function
name here
 pow(x,y) : this function returns
the (x)y module name is
not required
before function
name here
 ceil : this function return the x rounded
(x) to next
intege
r.
MATH MODULE FUNCTIONS
 floor(x) : this function returns the x
rounded to previous integer.
 fabs(x) : thisfunction returns absolute
value offloat x. absolute value means
number without any sign

 sin (x) : it return sine of x (measured


in radian)
MATH MODULE FUNCTIONS
 cos(x) : it return cosine of x (measured in
radian)

 tan(x) : it return tangent of x (measured in


radian)

 pi : return the constant value of pi


(22/7)

 e : return the constant value of


constant e

USING RANDOM MODULE
 Python has a module namely random that
provides random – number generators.
Random number means any number generated
within the given range.
 To generate random number in Python we have
to import random module
 2 most common method to generate random
number in python are :
 random() function
 randint(a,b) function
RANDOM() FUNCTION
 It is floating point random number
generator between 0.0 to 1.0. here
lower limit is inclusive where as upper
limit is less than 1.0.
 0<=N<1
 Examples:

Output is less
than 1
RANDOM() FUNCTION
 To generate random number between
given range of values using random(), the
following format should be used:
🞑 Lower_range + random() * (upper_range-
lower_range)
🞑 For example to generate number between 10
to 50:
 10 + random() * (40)
RANDINT() FUNCTION
 Another way to generate random
number is randint() function, but it generate
integer numbers.
 Both the given range values are inclusive i.e. if
we generate random number as :

🞑 randint(20,70)
 Inabove example random number between
20 to 70 will be taken. (including 20 and 70
also)
 What could be the minimum possible and
maximum possible numbers by following
code
import random
print([Link](3,1
0)-3)
 In a school fest, three randomly chosen
students out of 100 students (having roll
number 1 -100) have to present the
bouquet to the guests. Help the school
authorities choose three students randomly
Look at the following Python code and find 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 PICKER.
Note:
‐ Assume all the required header files are already being included
in the code.
‐ The function randint() generates an integer
between 1 to n import random
PICKER=1+[Link](0,2)
COLOR=[”BLUE”,”PINK”,”GREEN”,”RED”]
for I in range(1,PICKER+1):
for j in range(I+1):
print(COLOR[j],end=‘’)
print()
WHAT ARE THE POSSIBLE OUTCOME(S) EXECUTED FROM
THE FOLLOWING CODE? ALSO SPECIFY THE MAXIMUM
AND MINIMUM VALUES THAT CAN BE ASSIGNED TO
VARIABLE PICK

1) 2)
DELHIDELHI DELHI
MUMBAIMUMBAI DELHIMUMBAI
CHENNAICHENNAI DELHIMUMBAICHENNAI
KOLKATAKOLKATA
3) 4)
DELHI DELHI
MUMBAI DELHIMUMBAI
CHENNAI KOLKATAKOLKATAKOLKATA
KOKLATA
RANDRANGE() FUNCTION
 This function is also used to generate
random number within given range.
 Syntax
🞑 randrange(start,stop,step)
It will generate
random number
between 5 to 14

random output between 5 to 14,


may vary
RANDRANGE() FUNCTION

It will generate
random number
between 1 to 29
with stepping of 2
i.e. it will
generate number
with gap of 2 i.e.
1,3,5,7 and so on
STATISTICAL MODULE

 This provides functions for


module calculating statistics of
mathemati numeric (Real-valued)
 cal data.
We will deal with 3 basic function under
this module
🞑 Mean
🞑 Median
🞑 mode
MEAN
 The mean is the average of all
numbers and is sometimes called the
arithmetic mean.

55, is the average of all numbers in


the list
MEDIAN
 The median is the middle number in a
group of numbers.
With odd number
of elements it will
simply return the
middle position
value

With even number


of elements, it will
return the average
of value at (mid
+ mid-1)/2 i.e.
(50+60)/2 =
55.0
MODE

 The mode is the number that occurs most


often within a set of numbers i.e. most
common data in list.

Here, 10
occurs
most in the
list.
HOW TO CREATE A PYTHON LIBRARY(OUR
OWN MODULES AND PACKAGES)
Docstrings:These are the multiline comments
enclosed in triple quotes used for
documentation [Link] should be the first
string stored in the Python module.
Variables and constants: These are the labels
to store the data in them.
Statements:Single line code performing a
particular task.
Functions: Group of code combined together
with a name inorder to perform a particular
task.
HOW TO CREATE A PYTHON LIBRARY(OUR OWN
MODULES AND PACKAGES)
 Decide about the basic structure of

your package.
 Create the directory structure having

folders with names of package and sub


package.
 Create __init__.py file in a package and

sub package folders.(double


underscore).(Its mandatory to use this
__init__ file,it will specify that this is my
package and the modules in this
package can be imported)
STRUCTURE OF OUR PACKAGE

Geometry Package

[Link] [Link] Modules

Rectangle(
Cube() Functions
)
Cuboid()
Triangle()
For doing the above we have two methods.
1. Open the default Python folder---rightclick—
new folder—rename it as Geometry.
Or
2. Open Python prompt
>>> import os
>>>[Link](“Geometry”)
So here one folder with name “Geometry” will
be created in the python default path .
'''area module to find the area of various
geometrical shapes'''
def rectangle(l,b):
'''Used to find the area of
rectangle:length*breath'''
return l*b

def triangle(b,h):
'''Used to find the area of triangle:
(base*height)/2'''
return (b*h)/2
#Save it inside Geometry folder inside the
default python path
'''Volume module to find the volume of various
geometrical shapes'''
def cube(s):
'''Used to find the volume of cube:(side)^3'''
return s*s*s
def cuboid(l,b,h):
'''Used to find the volume cuboid:(l*b*h)'''
return (l*b*h)
#Save it inside Geometry folder inside the
default python path
#import Geometry package

import [Link] as a
import [Link] as v

print("Area of Rectangle is",[Link](5,6))


print("Area of Triangle is",[Link](5,6))

print("Volume of cube",[Link](5))
print("Area of cuboid is",[Link](5,6,2))

#Geometry package,Area module,rectangle() function


We can also import our module in Python cell.
>>>import [Link]
>>>help([Link])

>>> print([Link](8,6))
#This is called intellectual help or intelligent
help
 Now create a blanlk python file and save it
inside the geometry package with the file
name __init__.py(double
underscoreinitdouble
[Link]).Bydefault also it will take .py
 It differentiates our folder from other

package folder.
 We can have many folders there but the

folder which contains an init file it indicates


that it is a package.
 >>>import [Link]
 >>> [Link](5)

 78.53981633974483

 >>> help(geometry)

 >>> help(Geometry)

Output:
Help on package Geo:

NAME
Geo

PACKAGE CONTENTS
area
volume

FILE
c:\users\admin\appdata\local\programs\python\python38-32\geo\__init__.py
 >>>import random
 >>>help random

You might also like