1 of 43

CHAPTER - 03�PYTHON LIBRARIES

2 of 43

INTRODUCTION

3 of 43

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.

4 of 43

COMPONENTS OF PYTHON PROGRAM

5 of 43

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

6 of 43

ADVANTAGES OF MODULES

7 of 43

ADVANTAGES OF MODULES

1. Reusability

2. Clarity

3. Classification/ Grouping of code

4. Easy to understand

8 of 43

IMPORTING MODULES

9 of 43

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

10 of 43

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

11 of 43

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)

12 of 43

IMPORTING MODULES- import

13 of 43

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

14 of 43

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.

15 of 43

NAMESPACES

16 of 43

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

17 of 43

NAMESPACES

Built in name space

Global name space

Local name space

18 of 43

NAME RESOLUTION

19 of 43

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

20 of 43

NAME RESOLUTION

Built in name space

Global name space

Local name space

Enclosed

21 of 43

MODULE ALIASING

22 of 43

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)

23 of 43

MEMBER ALIASING

24 of 43

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.

25 of 43

PACKAGE/LIBRARY

26 of 43

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

27 of 43

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

28 of 43

PACKAGE/LIBRARY

GEOMETRY

Area.py

Volume.py

FOLDER

FILES

29 of 43

PACKAGE/LIBRARY

FOLDER IS CREATED

30 of 43

PACKAGE/LIBRARY

AREA MODULE IS CREATED

31 of 43

PACKAGE/LIBRARY

VOLUME MODULE IS CREATED

32 of 43

PACKAGE/LIBRARY

CREATING __init__.py FILE

33 of 43

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

34 of 43

LOCATING MODULES

35 of 43

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

36 of 43

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

37 of 43

pip

38 of 43

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

39 of 43

PYTHON STANDARD LIBRARY

40 of 43

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

41 of 43

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

42 of 43

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

43 of 43

Thank You