CHAPTER - 03�PYTHON LIBRARIES
INTRODUCTION
INTRODUCTION
A Module is a file containing python definitions, functions, variables, classes and statements with .py extension.
Python package is a directory of python module.
A Library is a collection of various packages. There is no difference between library and python package. Library is used to loosely describe a collection of core or main modules.
COMPONENTS OF PYTHON PROGRAM
COMPONENTS OF PYTHON PROGRAM
A Module is a file that contains python code.
The python program comprises of three main components
i) Library or Package
ii) Module
iii) Functions/Sub-Modules
ADVANTAGES OF MODULES
ADVANTAGES OF MODULES
1. Reusability
2. Clarity
3. Classification/ Grouping of code
4. Easy to understand
IMPORTING MODULES
IMPORTING MODULES
t
Python module files have an extension .py
These modules can be imported in the following ways:
1) import statement
2) from statement
3) from * statement
IMPORTING MODULES- import
t
import statement is used to include the modules in other programs.
syntax : import <filename>
example: import math
more than one module can be inserted in a python program
syntax : import <filename> ,<filename>, <filename>……..
for example: import math,os
IMPORTING MODULES- import
t
using import statement one can view all the functions and other attributes of a particular module
for example: import math
dir(math)
IMPORTING MODULES- import
IMPORTING MODULES- from
t
importing module can be done using from statement specific attributes can be included in other programs.
syntax :
from <filename> import function name
example:
from math import math.sqrt
IMPORTING MODULES- from*
t
from* statement can be used to import all names from the module in to the current calling name space.
syntax :
from <filename> import *
example:
from math import *
math.sqrt(4)
we can access any function by using dot notation.
NAMESPACES
NAMESPACES
t
When we import modules in a particular program these modules will become part of that program and are called as namespace.
Python impliments namespaces in the form of dictionaries. It maintains a name to object mapping.
There are three types of namespaces
1) Global
2) Local
3) Built in
NAMESPACES
Built in name space
Global name space
Local name space
NAME RESOLUTION
NAME RESOLUTION
t9lo
Already we know the scope rules of python programming.
For every name reference within a program when you access a variable python follows name resolution rule i.e LEGB (Local, Enclosed, Global, Built-in)
Contd.. Next slide
NAME RESOLUTION
Built in name space
Global name space
Local name space
Enclosed
MODULE ALIASING
MODULE ALIASING
t9lo
One can create an alias while importing module in a program
syntax:
import <filename> as <alias name>
for example: import math as m
m.sqrt(4)
MEMBER ALIASING
MEMBER ALIASING
t9lo
Like module aliasing members are also aliased
syntax:
import <filename> as <alias name>, member as alias name
for example: import test as t, add as sum
test.py is module file and is referred to as t and add is the function, it is referred to as sum.
PACKAGE/LIBRARY
PACKAGE/LIBRARY
t9lo
Python packages are the collection of related modules. You can import a package or create your own.
The main difference between a module and a package is that package is a collection of modules and has an __init__.py file
PACKAGE/LIBRARY
t9lo
Python package is a simply directory of python modules
Steps to create and import a package
1. create a directory named ‘Gemetry’
2. add modules area.py and volume.py
3. create a file __init__.py in directory ‘Geometry’. The __init__.py files are
required to make python treat the
directory as containing package
PACKAGE/LIBRARY
GEOMETRY
Area.py
Volume.py
FOLDER
FILES
PACKAGE/LIBRARY
FOLDER IS CREATED
PACKAGE/LIBRARY
AREA MODULE IS CREATED
PACKAGE/LIBRARY
VOLUME MODULE IS CREATED
PACKAGE/LIBRARY
CREATING __init__.py FILE
PACKAGE/LIBRARY
__init__.py FILE
What is __init__.py file?
__init__.py is simply a file used to consider directories on the disk as package of python.
It is basically used to initilize the python package
LOCATING MODULES
PACKAGE/LIBRARY
Python searches module in the following manner
PACKAGE/LIBRARY
Python searches module in the following manner
pip
What is pip?
pip is a package-management system used to install and manage software packages written in Python.
To check pip version run,
pip --version at dos prompt
PYTHON STANDARD LIBRARY
PYTHON STANDARD LIBRARY
DATE AND TIME MODULE.
import datetime
v_date=datetime.date.today()
vyear = v_date.year()
vmonth = v_date.month()
vday = v_date.day()
PYTHON STANDARD LIBRARY
DATE AND TIME MODULE.
import datetime
v_date=datetime.date.today()
vnow = v_date.now()
vhour = v_date.hour()
vmin = v_date.minute()
vsec = v_date.second()
CLASS TEST
1. What are the components of python program.
2.Explain the ways to import a module in python program.
3.What is namespace? Explain in detail
4. What is python package? Write down the steps to create a python package and also write a programs and create a package.
Class : XII Time: 40 Min
Topic: Python Libraries Max Marks: 40
Each Question carries 5 Marks
Thank You