You are on page 1of 27

Using Python

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

Module: It is a file containing python definitions,


functions, variables, classes and statements
with .py extension.
Python package: it is a directory of python
modules.
Library: it is a collection of various packages.
Modules in python

A module is a file consisting of python


code that can define functions, classes
and variables related to particular task.
It allows us to organize our code by
grouping related code which makes the
code easier to understand and use.
Advantages of Python Modules:

We can import the module functionality


It provides the facility of code reusability
It helps to logically organize our python code.
It makes the code easier to understand and use.
Similar types of attributes can be placed in a single
module.
Types of Modules:

Pre – Defined modules


User defined modules
Pre defined modules:
The modules which are already defined in the
python library are referred to as pre defined
modules.
Various examples of pre defined modules are
math module
string module
random module
Importing python modules
We can import the modules by using
 the import keyword
Import with renaming
By using from...import statement
Import all names
Using import keyword
We can import the modules by using the import statement
syntax:
import module1 [, module2,….]

eg. import math

we can access various functions in the math module by using


dot operator
eg.
result=math.sqrt(64)
Import with renaming

We can import a module by


renaming it as follows:
Python from...import statement

We can also import a specific attribute of a module by using from…import


statement also.
Syntax:
from <module_name> import <function_name1 > [, <function_name2>,….]
We could have imported multiple
objects as follows.
We could have imported all objects of
module

Syntax: from <modulename> import *

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

Importing module area in math1.py

import area The import statem


attributes and m
print(area.circle_area(5)) mo
print(area.square_area(10)) Methods in a modu
print(area.rectangle_area(20,40)) syn
module
The dir() built-in function

We can use the dir() function to find out names that are


defined inside a module.
For example, to retrieve the listing of all the objects present
inside the user created module “area”
import versus from…import
we should avoid using from statement and we should always prefer to use import
statement because it may lead to name clashes as explained below:

import statement from statement


 When we import a module using  When we import a module using
import statement the code of from statement the code of
imported module is interpreted imported module is interpreted
and executed and executed
 The functions and variables  The functions and variables
defined in the module are now defined in the module are now
available to the imported module available to the imported module

 For imported module, a new  No new namespace is created , the


namespace is setup with the same imported definition is just added in
name as that of the module the current namespace.
  
  
Namespaces
A namespace is a simple system to control the names
in a program. It ensures that names are unique and
won’t lead to any conflict.
Python implements namespaces in the form of
dictionaries. It maintains a name-to-object mapping
where names act as keys and the objects as values.
Multiple namespaces may have the same name but
point to a different variable.
Check out a few examples of namespaces for more
clarity.
Difference b/w import statement and from…import
statement
import test from test import t1
Original Namespace
Current Namespace Current Namespace is implicitly
Programs
Namespace created for
imported
test t1
function t1

No new space created for imported


test_Dict Namespace
object. It is just added to original
created for
t1 program’s namespace
imported
Modules Namespace module
Name Resolution (Resolving Scope of a
Name)
When we access a variable from within
a program or function, python follows
name resolution rule, known as LEGB
Rule. Python does the following steps
to resolve it :
Built- in

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”)

After this we will create all the related modules


and place these different modules under this
directory say area.py and volume.py
Create another file _init_.py file and
also place it under geometry
directory
Now we can import package and
use the attributes
How does python locate the
modules
When we import a module, python interpreter searches for
the module in the following sequence:
The current directory
If module is not found in current directory, python then
searches each directory in the shell variable
PYTHONPATH(it is the variable which tells the interpreter
where to locate the module files imported into a program)
If all else fail, python checks the default path, which is the
location where python has been installed on our computer
system.
Standard Python Libraries
DateTime Library / Module
Python supports yyyy-mm-dd format for
displaying the date.
This library has various classes:
date class: today(), year(), month(), day()
Time class: now(),hour() minute(), second()
Thank You.

You might also like