SlideShare a Scribd company logo
1 of 24
Download to read offline
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

What's hot

Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesAndrew Ferlitsch
 
introduction to python
 introduction to python introduction to python
introduction to pythonJincy Nelson
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in pythonTMARAGATHAM
 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Pedro Rodrigues
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaEdureka!
 

What's hot (20)

Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Python for loop
Python for loopPython for loop
Python for loop
 
introduction to python
 introduction to python introduction to python
introduction to python
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Tuples in Python
Tuples in PythonTuples in Python
Tuples in Python
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Python libraries
Python librariesPython libraries
Python libraries
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
Numpy
NumpyNumpy
Numpy
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Python
PythonPython
Python
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Introduction to the Python
Introduction to the PythonIntroduction to the Python
Introduction to the Python
 
Python ppt
Python pptPython ppt
Python ppt
 

Similar to USE PYTHON LIBRARIES

Interesting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packagesInteresting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packagesarunavamukherjee9999
 
Modules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptxModules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptxarunavamukherjee9999
 
Python Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxPython Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxSingamvineela
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxRohitKumar639388
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptxkrushnaraj1
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptxAshwini Raut
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptxvishnupriyapm4
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptxvishnupriyapm4
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsRuth Marvin
 
pythonlibrariesandmodules-210530042906.docx
pythonlibrariesandmodules-210530042906.docxpythonlibrariesandmodules-210530042906.docx
pythonlibrariesandmodules-210530042906.docxRameshMishra84
 
Interview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfInterview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfExaminationSectionMR
 
jb_Modules_in_Python.ppt
jb_Modules_in_Python.pptjb_Modules_in_Python.ppt
jb_Modules_in_Python.pptloliktry
 

Similar to USE PYTHON LIBRARIES (20)

Interesting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packagesInteresting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packages
 
Modules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptxModules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptx
 
packages.pptx
packages.pptxpackages.pptx
packages.pptx
 
Python Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxPython Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptx
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptx
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptx
 
Python training
Python trainingPython training
Python training
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Python modules
Python modulesPython modules
Python modules
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
 
Chapter - 4.pptx
Chapter - 4.pptxChapter - 4.pptx
Chapter - 4.pptx
 
ch 2. Python module
ch 2. Python module ch 2. Python module
ch 2. Python module
 
Python for Beginners
Python  for BeginnersPython  for Beginners
Python for Beginners
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
pythonlibrariesandmodules-210530042906.docx
pythonlibrariesandmodules-210530042906.docxpythonlibrariesandmodules-210530042906.docx
pythonlibrariesandmodules-210530042906.docx
 
Interview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfInterview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdf
 
Python modules
Python modulesPython modules
Python modules
 
jb_Modules_in_Python.ppt
jb_Modules_in_Python.pptjb_Modules_in_Python.ppt
jb_Modules_in_Python.ppt
 

Recently uploaded

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Recently uploaded (20)

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

USE PYTHON LIBRARIES

  • 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