0% found this document useful (0 votes)
35 views43 pages

Chapter - 03 Python Libraries

This document discusses Python libraries and modules. It covers the following key points: 1. A module is a file containing Python code and definitions, while a package is a directory containing modules. Libraries are collections of packages. 2. The main components of a Python program are libraries/packages, modules, and functions. 3. Modules can be imported using import, from, and from * statements. This allows the code to be reused. 4. Namespaces and name resolution follow LEGB rules - looking up names in local, enclosed, global, and built-in namespaces.

Uploaded by

aarthi dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views43 pages

Chapter - 03 Python Libraries

This document discusses Python libraries and modules. It covers the following key points: 1. A module is a file containing Python code and definitions, while a package is a directory containing modules. Libraries are collections of packages. 2. The main components of a Python program are libraries/packages, modules, and functions. 3. Modules can be imported using import, from, and from * statements. This allows the code to be reused. 4. Namespaces and name resolution follow LEGB rules - looking up names in local, enclosed, global, and built-in namespaces.

Uploaded by

aarthi dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

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

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

import statement is used to include the modules


in other programs.
syntax : import
<filename> example: import
math can be
morethan one module
inserted
syntax
in a: python
importprogram
<filename> ,<filename>,
<filename>……..
for example: import math,os
IMPORTING MODULES- import

using import statement one can view all the


functions and attributes of particular
other module a
for example:
import math
dir(math)
IMPORTING MODULES- import
IMPORTING MODULES- from

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*

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
NAMESPACE
S
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

Enclosed

Local name space


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
referred to as t and addand
is theisfunction, 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

FOLDER
GEOMETRY

Area.py Volume.p
y
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

initinit.py .py file? a file used to


is simply
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
1) Searches in current directory
2) If the module is not found then
searches each directory in the shell
variable PYTHONPATH
3) If all else fails, python checks the
default path which is the installation
location of the python
PACKAGE/LIBRARY

Pythonsearches module in the


following manner
1) Searches in current directory
2) If the module is not found then
searches each directory in the shell
variable PYTHONPATH
3) If all else fails, python checks the
default path which is the installation
location of the python
pip
What is pip?

pip a package-management
is system to install and
used
software packages
manage
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
Class : XII Time: 40 Min
Topic: Python Libraries Max Marks: 40
Each Question carries 5 Marks
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.
Thank You

You might also like