SlideShare a Scribd company logo
2
Most read
5
Most read
7
Most read
USING PYTHON
LIBRARIES
Components of Python program
- Library or Package
- Module
- Functions
A large and complex program is divided
into functions.
Interrelated functions are stored together
in a module.
Interrelated modules are placed under a
common namespace known as a package
❏ Module - A module is a file containing python
statements, variables, function definitions and
classes. The extension of this file is .py.
❏ Package - It is a directory (folder) of python
modules. A package is a collection of related
modules that work together to provide certain
functionality.
❏ Library - It is collection of many packages in
Python. Generally there is no difference
between python package and python libraries.
Relation between Module, Package
and Library
Python Libraries
A library is a collection of modules and
packages that together cater to a specific
type of applications or requirement.
It contains bundles of code that can be used
repeatedly in different programs. It makes
Python Programming simpler and
convenient for the programmer. As we don't
need to write the same code again and again
for different programs.
Python Standard Library - This library is distributed
with Python that contains modules for various types of
functionalities.
Numpy Library - This library provides some advance
math functionalities along with tools to create and
manipulate numeric arrays.
Scipy library - This is another useful library that offers
algorithmic and mathematical tools for scientific
calculations
tkinter library - This library provides graphical user
interface toolkit which helps you to create
user-friendly GUI interface for different types of
applications.
Matplotlib library - This library offers many functions
and tools to produce different types of graphs and
charts.
Commonly used Python Libraries
It is written in C, and handles functionality like
I/O and other core modules.
● math module - which provides mathematical
functions for performing different types of
calculations
● cmath module - which provides
mathematical functions for complex numbers
● random module - which provides functions
for generating pseudo-random numbers
● statistics module - which provides
mathematical statistics functions
● Urllib module - which provides URL handling
functions so that you can access websites from
within your program
Standard Library
A Python module is a file (.py file) containing
variables, Python definitions and statements.
A module can define functions, classes, and
variables. A module can also include runnable
code. Grouping related code into a module
makes the code easier to understand and use.
In Python, Modules are simply files with the
“.py” extension containing variables,
statements, functions, class definitions and
which can be imported inside another Python
Program.
Module
❏ docstrings - triple quoted comments; useful for
documentation purposes. For documentation,
the docstrings should be the first string stored
inside a module/function body/class.
❏ variables and constants - labels for data
❏ classes - templates/blueprint to create objects
of same kind.
❏ objects - instances of classes. In python all type
of data is treated as object of some class.
❏ statements - instructions
❏ functions - named group of instructions
Parts of a module
""" Circle(x, y, r) - 2D Shape where x, y are center
and r is the radius"""
shape_name ="Circle"
def area(rad):
'''returns area of circle - accepts radius as
parameter '''
return 3.14*rad*rad
def peri(rad):
'''return perimeter of circle - accepts radius as
parameter '''
return 2*3.14*rad
Creating module circle.py
""" rectangle(len,wid) - 2D Shape where len and wid are
the length and width of a rectangle"""
#constants
shape_name ="Rectangle"
def area(l,b):
'''returns area of rectangle - accepts length and width as
parameters '''
return l*b
def peri(l,b):
'''return perimeter of rectangle - accepts length and
width as parameters '''
return 2*(l+b)
Creating module rectangle.py
>> import circle
>> help(circle)
NAME
circle - Circle(x, y, r) - 2D Shape where x, y are center and r
is the radius
FUNCTIONS
area(rad)
returns area of circle - accepts radius as parameter
peri(rad)
return perimeter of circle - accepts radius as parameter
DATA
shape_name = 'Circle'
FILE c:usersuserprogramspythonpython38circle.py
To view the documentation of module
circle
>> import rectangle
>> help(rectangle)
Help on module rectangle:
NAME
rectangle - rectangle(len,wid) - 2D Shape where len and
wid are the length and width of a rectangle
FUNCTIONS
area(l, b)
returns area of rectangle - accepts length and width as
parameters
peri(l, b)
return perimeter of rectangle - accepts length and width
as parameters
DATA
shape_name = 'Rectangle'
To view the documentation of module
rectangle
You can use any Python source file as a module by
executing an import statement in some other Python
source file.
The import has the following syntax −
import module1[, module2[,... moduleN]
When the interpreter encounters an import statement, it
imports the module if the module is present in the
search path. A search path is a list of directories that the
interpreter searches before importing a module.
Example
import math, random
print(math.pi)
print(random.randint(10,22))
Import statement
We can import a module by renaming it as follows:
# import module by renaming it
import math as m
print("The value of pi is", m.pi)
We have renamed the math module as m. This can
save us typing time in some cases.
Note that the name math is not recognized in our
scope. Hence, math.pi is invalid, and m.pi is the
correct implementation.
Import with renaming
Python's from statement lets you import specific attributes
from a module into the current namespace.
The from...import has the following syntax −
from modname import name1[, name2[, ... nameN]]
For example
from math import sqrt, log10
print(sqrt(144))
print(log10(1000))
print(ceil(13.4)) #will cause NameError
This statement does not import the entire module math into
the current namespace; it just introduces the sqrt, log10 from
the module math into the global symbol table of the importing
module.
The from...import Statement
It is also possible to import all names from a module into
the current namespace by using the following import
statement −
from modname import *
This provides an easy way to import all the items from a
module into the current namespace;
The from...import * Statement
When you import a module, the Python interpreter
searches for the module in the following sequences −
1. The current directory.
2. If the module isn't found, Python then searches each
directory in the shell variable PYTHONPATH.
3. If all else fails, Python checks the default path.
4. The module search path is stored in the system
module sys as the sys.path variable. The sys.path
variable contains the current directory,
PYTHONPATH, and the installation-dependent
default.
import sys
for path in sys.path:
print(path)
Locating Modules
We don't usually store all of our files on our computer
in the same location. We use a well-organized
hierarchy of directories for easier access.
Similar files are kept in the same directory, for example,
we may keep all the songs in the "music" directory.
Analogous to this, Python has packages for directories
and modules for files.
A Python module may contain several classes,
functions, variables, etc. whereas a Python package can
contains several module. In simpler terms a package is
folder that contains various modules as files.
Python Package
As our application program grows larger in size with a
lot of modules, we place similar modules in one
package and different modules in different packages.
This makes a project (program) easy to manage and
conceptually clear.
Similarly, as a directory can contain subdirectories
and files, a Python package can have sub-packages
and modules.
A directory must contain a file named __init__.py in
order for Python to consider it as a package. This file
can be left empty but we generally place the
initialization code for that package in this file.
Python Package
Example
Let’s create a package named mypckg that will contain
two modules mod1 and mod2. To create this module
follow the below steps –
Create a folder named mypckg.
Inside this folder create an empty Python file named
__init__.py
Then create two modules mod1 and mod2 in this folder.
mod1.py
def msg():
print("Welcome to DPS")
mod2.py
def sum(a, b):
return a+b
Creating Package
Hierarchy Package
Syntax:
import package_name.module_name
#importing all functions of a module
from mypckg import mod1
from mypckg import mod2
mod1.msg()
res = mod2.sum(1, 2)
print(res)
Import Modules from a Package
Syntax:
from package_name import module.fun_name
#importing specific function from module
from mypckg.mod1 import msg
from mypckg.mod2 import sum
msg()
res = sum(1, 2)
print(res)
Import Module Functions from a Package

More Related Content

PDF
Life cycle-of-a-thread
javaicon
 
PPTX
Functions in python
colorsof
 
PPT
Packages in java
Abhishek Khune
 
PDF
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
PPTX
Python SQite3 database Tutorial | SQlite Database
ElangovanTechNotesET
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PDF
Set methods in python
deepalishinkar1
 
PPTX
Functions in Python
Shakti Singh Rathore
 
Life cycle-of-a-thread
javaicon
 
Functions in python
colorsof
 
Packages in java
Abhishek Khune
 
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Python SQite3 database Tutorial | SQlite Database
ElangovanTechNotesET
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
Set methods in python
deepalishinkar1
 
Functions in Python
Shakti Singh Rathore
 

What's hot (20)

PPTX
Inheritance
Tech_MX
 
PPTX
Python Programming Essentials - M23 - datetime module
P3 InfoTech Solutions Pvt. Ltd.
 
PPTX
Python: Polymorphism
Damian T. Gordon
 
PPTX
Polymorphism in java
Janu Jahnavi
 
PPTX
Packages In Python Tutorial
Simplilearn
 
PPTX
Clean Architecture
Zahra Heydari
 
PPTX
Access modifier and inheritance
Dikshyanta Dhungana
 
PPTX
Error handling and debugging in vb
Salim M
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPT
Serialization/deserialization
Young Alista
 
PPTX
Collections and its types in C# (with examples)
Aijaz Ali Abro
 
PPTX
Package in Java
lalithambiga kamaraj
 
PPT
Active x control
Amandeep Kaur
 
PPTX
Inheritance in java
RahulAnanda1
 
PPTX
Object Oriented Programming in Python
Sujith Kumar
 
PPTX
Operator Overloading In Python
Simplilearn
 
PPTX
Method overloading
Lovely Professional University
 
PDF
What is Python Lambda Function? Python Tutorial | Edureka
Edureka!
 
PPTX
Operator Overloading & Function Overloading
Meghaj Mallick
 
PPTX
Unit Testing with Python
MicroPyramid .
 
Inheritance
Tech_MX
 
Python Programming Essentials - M23 - datetime module
P3 InfoTech Solutions Pvt. Ltd.
 
Python: Polymorphism
Damian T. Gordon
 
Polymorphism in java
Janu Jahnavi
 
Packages In Python Tutorial
Simplilearn
 
Clean Architecture
Zahra Heydari
 
Access modifier and inheritance
Dikshyanta Dhungana
 
Error handling and debugging in vb
Salim M
 
Inheritance in c++
Vineeta Garg
 
Serialization/deserialization
Young Alista
 
Collections and its types in C# (with examples)
Aijaz Ali Abro
 
Package in Java
lalithambiga kamaraj
 
Active x control
Amandeep Kaur
 
Inheritance in java
RahulAnanda1
 
Object Oriented Programming in Python
Sujith Kumar
 
Operator Overloading In Python
Simplilearn
 
Method overloading
Lovely Professional University
 
What is Python Lambda Function? Python Tutorial | Edureka
Edureka!
 
Operator Overloading & Function Overloading
Meghaj Mallick
 
Unit Testing with Python
MicroPyramid .
 
Ad

Similar to Using Python Libraries.pdf (20)

PPTX
Python module 3, b.tech 5th semester ppt
course5325
 
PDF
Python libraries
Prof. Dr. K. Adisesha
 
PPTX
Class 12 CBSE Chapter: python libraries.pptx
AravindVaithianadhan
 
PPTX
Interesting Presentation on Python Modules and packages
arunavamukherjee9999
 
PPTX
Modules and Packages in Python Programming Language.pptx
arunavamukherjee9999
 
PDF
Python. libraries. modules. and. all.pdf
prasenjitghosh1998
 
PPTX
packages.pptx
SHAIKIRFAN715544
 
PPTX
Python Modules, executing modules as script.pptx
Singamvineela
 
PPTX
Using python libraries.pptx , easy ppt to study class 12
anikedheikhamsingh
 
PPTX
pythontraining-201jn026043638.pptx
RohitKumar639388
 
PDF
Functions_in_Python.pdf text CBSE class 12
JAYASURYANSHUPEDDAPA
 
PPTX
CLASS-11 & 12 ICT PPT Functions in Python.pptx
seccoordpal
 
PPTX
Functions_in_Python.pptx
krushnaraj1
 
PPTX
Python training
Kunalchauhan76
 
PDF
Unit-2 Introduction of Modules and Packages.pdf
Harsha Patil
 
PPTX
Chapter 03 python libraries
Praveen M Jigajinni
 
PPTX
Functions and Modules.pptx
Ashwini Raut
 
PDF
W-334535VBE242 Using Python Libraries.pdf
manassingh1509
 
PPTX
Chapter - 4.pptx
MikialeTesfamariam
 
Python module 3, b.tech 5th semester ppt
course5325
 
Python libraries
Prof. Dr. K. Adisesha
 
Class 12 CBSE Chapter: python libraries.pptx
AravindVaithianadhan
 
Interesting Presentation on Python Modules and packages
arunavamukherjee9999
 
Modules and Packages in Python Programming Language.pptx
arunavamukherjee9999
 
Python. libraries. modules. and. all.pdf
prasenjitghosh1998
 
packages.pptx
SHAIKIRFAN715544
 
Python Modules, executing modules as script.pptx
Singamvineela
 
Using python libraries.pptx , easy ppt to study class 12
anikedheikhamsingh
 
pythontraining-201jn026043638.pptx
RohitKumar639388
 
Functions_in_Python.pdf text CBSE class 12
JAYASURYANSHUPEDDAPA
 
CLASS-11 & 12 ICT PPT Functions in Python.pptx
seccoordpal
 
Functions_in_Python.pptx
krushnaraj1
 
Python training
Kunalchauhan76
 
Unit-2 Introduction of Modules and Packages.pdf
Harsha Patil
 
Chapter 03 python libraries
Praveen M Jigajinni
 
Functions and Modules.pptx
Ashwini Raut
 
W-334535VBE242 Using Python Libraries.pdf
manassingh1509
 
Chapter - 4.pptx
MikialeTesfamariam
 
Ad

Recently uploaded (20)

PDF
High Ground Student Revision Booklet Preview
jpinnuck
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
High Ground Student Revision Booklet Preview
jpinnuck
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 

Using Python Libraries.pdf

  • 2. Components of Python program - Library or Package - Module - Functions A large and complex program is divided into functions. Interrelated functions are stored together in a module. Interrelated modules are placed under a common namespace known as a package
  • 3. ❏ Module - A module is a file containing python statements, variables, function definitions and classes. The extension of this file is .py. ❏ Package - It is a directory (folder) of python modules. A package is a collection of related modules that work together to provide certain functionality. ❏ Library - It is collection of many packages in Python. Generally there is no difference between python package and python libraries. Relation between Module, Package and Library
  • 4. Python Libraries A library is a collection of modules and packages that together cater to a specific type of applications or requirement. It contains bundles of code that can be used repeatedly in different programs. It makes Python Programming simpler and convenient for the programmer. As we don't need to write the same code again and again for different programs.
  • 5. Python Standard Library - This library is distributed with Python that contains modules for various types of functionalities. Numpy Library - This library provides some advance math functionalities along with tools to create and manipulate numeric arrays. Scipy library - This is another useful library that offers algorithmic and mathematical tools for scientific calculations tkinter library - This library provides graphical user interface toolkit which helps you to create user-friendly GUI interface for different types of applications. Matplotlib library - This library offers many functions and tools to produce different types of graphs and charts. Commonly used Python Libraries
  • 6. It is written in C, and handles functionality like I/O and other core modules. ● math module - which provides mathematical functions for performing different types of calculations ● cmath module - which provides mathematical functions for complex numbers ● random module - which provides functions for generating pseudo-random numbers ● statistics module - which provides mathematical statistics functions ● Urllib module - which provides URL handling functions so that you can access websites from within your program Standard Library
  • 7. A Python module is a file (.py file) containing variables, Python definitions and statements. A module can define functions, classes, and variables. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use. In Python, Modules are simply files with the “.py” extension containing variables, statements, functions, class definitions and which can be imported inside another Python Program. Module
  • 8. ❏ docstrings - triple quoted comments; useful for documentation purposes. For documentation, the docstrings should be the first string stored inside a module/function body/class. ❏ variables and constants - labels for data ❏ classes - templates/blueprint to create objects of same kind. ❏ objects - instances of classes. In python all type of data is treated as object of some class. ❏ statements - instructions ❏ functions - named group of instructions Parts of a module
  • 9. """ Circle(x, y, r) - 2D Shape where x, y are center and r is the radius""" shape_name ="Circle" def area(rad): '''returns area of circle - accepts radius as parameter ''' return 3.14*rad*rad def peri(rad): '''return perimeter of circle - accepts radius as parameter ''' return 2*3.14*rad Creating module circle.py
  • 10. """ rectangle(len,wid) - 2D Shape where len and wid are the length and width of a rectangle""" #constants shape_name ="Rectangle" def area(l,b): '''returns area of rectangle - accepts length and width as parameters ''' return l*b def peri(l,b): '''return perimeter of rectangle - accepts length and width as parameters ''' return 2*(l+b) Creating module rectangle.py
  • 11. >> import circle >> help(circle) NAME circle - Circle(x, y, r) - 2D Shape where x, y are center and r is the radius FUNCTIONS area(rad) returns area of circle - accepts radius as parameter peri(rad) return perimeter of circle - accepts radius as parameter DATA shape_name = 'Circle' FILE c:usersuserprogramspythonpython38circle.py To view the documentation of module circle
  • 12. >> import rectangle >> help(rectangle) Help on module rectangle: NAME rectangle - rectangle(len,wid) - 2D Shape where len and wid are the length and width of a rectangle FUNCTIONS area(l, b) returns area of rectangle - accepts length and width as parameters peri(l, b) return perimeter of rectangle - accepts length and width as parameters DATA shape_name = 'Rectangle' To view the documentation of module rectangle
  • 13. You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax − import module1[, module2[,... moduleN] When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. Example import math, random print(math.pi) print(random.randint(10,22)) Import statement
  • 14. We can import a module by renaming it as follows: # import module by renaming it import math as m print("The value of pi is", m.pi) We have renamed the math module as m. This can save us typing time in some cases. Note that the name math is not recognized in our scope. Hence, math.pi is invalid, and m.pi is the correct implementation. Import with renaming
  • 15. Python's from statement lets you import specific attributes from a module into the current namespace. The from...import has the following syntax − from modname import name1[, name2[, ... nameN]] For example from math import sqrt, log10 print(sqrt(144)) print(log10(1000)) print(ceil(13.4)) #will cause NameError This statement does not import the entire module math into the current namespace; it just introduces the sqrt, log10 from the module math into the global symbol table of the importing module. The from...import Statement
  • 16. It is also possible to import all names from a module into the current namespace by using the following import statement − from modname import * This provides an easy way to import all the items from a module into the current namespace; The from...import * Statement
  • 17. When you import a module, the Python interpreter searches for the module in the following sequences − 1. The current directory. 2. If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH. 3. If all else fails, Python checks the default path. 4. The module search path is stored in the system module sys as the sys.path variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation-dependent default. import sys for path in sys.path: print(path) Locating Modules
  • 18. We don't usually store all of our files on our computer in the same location. We use a well-organized hierarchy of directories for easier access. Similar files are kept in the same directory, for example, we may keep all the songs in the "music" directory. Analogous to this, Python has packages for directories and modules for files. A Python module may contain several classes, functions, variables, etc. whereas a Python package can contains several module. In simpler terms a package is folder that contains various modules as files. Python Package
  • 19. As our application program grows larger in size with a lot of modules, we place similar modules in one package and different modules in different packages. This makes a project (program) easy to manage and conceptually clear. Similarly, as a directory can contain subdirectories and files, a Python package can have sub-packages and modules. A directory must contain a file named __init__.py in order for Python to consider it as a package. This file can be left empty but we generally place the initialization code for that package in this file. Python Package
  • 21. Let’s create a package named mypckg that will contain two modules mod1 and mod2. To create this module follow the below steps – Create a folder named mypckg. Inside this folder create an empty Python file named __init__.py Then create two modules mod1 and mod2 in this folder. mod1.py def msg(): print("Welcome to DPS") mod2.py def sum(a, b): return a+b Creating Package
  • 23. Syntax: import package_name.module_name #importing all functions of a module from mypckg import mod1 from mypckg import mod2 mod1.msg() res = mod2.sum(1, 2) print(res) Import Modules from a Package
  • 24. Syntax: from package_name import module.fun_name #importing specific function from module from mypckg.mod1 import msg from mypckg.mod2 import sum msg() res = sum(1, 2) print(res) Import Module Functions from a Package