Python uses modules, packages, and libraries to organize code. A module is a .py file containing functions, classes, and variables. Related modules are grouped into packages, which are directories containing an __init__.py file. Libraries are collections of packages that provide specific functionality. The Python standard library includes common modules like math and random. Modules can be imported and used to reuse code in other files or packages.
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
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