Using Python Libraries
Using Python Libraries
Libraries
B Y: - M S . G U R J I N D E R K A U R
PGT CS
KV PUSHPVIHAR
Python Program comprises
of three components
Library or package
Module
Functions/sub modules
Relation between a module, package and library in
python
Example:
from math import *
User Defined Modules
The modules which are created by the user are
referred to as User Defined Modules.
We can create a user defined module by saving any
file with .py extension.
Eg.
We have created a python file called area.py which
has few functions it for calculating the area of a
circle, square and a rectangle.
area.py
import math
def circle_area(radius):
return math.pi*radius*radius
def square_area(side):
return side*side
def rectangle_area(length, breadth):
return length*breadth
Global
Enclosed
Local
Package / Library
It is a directory of python modules.
In other words we can say that a package is a
collection of related modules.
We can import a package or create our own.
The main difference between a module and a
package is that a package is a collection of
modules and has an _init_.py file.
Steps to create and import
package
1. Create a directory named geometry and add both
modules area.py and volume.py in it.
2. Also create a file _init_.py in the directory
geometry
3. The _init_.py files are required to make python
treat the directories as containing packages.
Basically it is used to initialize the python packages
4. Import the package and use the attributes
contained inside the package.
Creating the Directory
Write the following commands in the python
command prompt
>>>import os
>>>os.mkdir(“Geometry”)