0% found this document useful (0 votes)
6 views18 pages

4 Using Python Libraries

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)
6 views18 pages

4 Using Python Libraries

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/ 18

Using Python Libraries

CHAPTER - 4
Python Libraries
• Frequently used modules are generally
known as libraries which contain code for
general purpose.
• These libraries are the collection of
methods, classes which can be used
easily.
• Python program is made using these
components. -
– Framework
– Library
– package
– Module
Relation Between Python Libraries,
Module and Package
• A module is a file containing
python definition, functions,
variables, classes and
statements. The extension of
this file is “.py”.
• While Python package is a
directory(folder) of python
modules, a library is
collection of many packages
in python. Generally there is
no difference between
python package and python
library.
Module in
• A Python
module is a file containing python
definition, functions, variables, classes and
statements. The extension of this file is
“.py”.
• The name of module is the name of .py file
e.g. in math.py the module name is math.
And _name_ variable is used to store this
module.
• Advantages–
– Its biggest advantage is that we can import its
functionality in any program and use it.
– Reusability is one of the biggest advantages.
– It helps in logically organization of Python code.
– Programming becomes very easy when we use the
collection of
same types of codes.
Processing of import <module>
command
• When you run the import <module> command then the
following things occur -
1. Imported module’s code gets executed after interpretation.
2. All the programs and variables of imported module are
present in the program.
3. A namespace setup is created for the imported module in the
name
modul of
e.
Processing of from <module> import
<object> command
• When you run the from<module> import <object> command
then the following things occur -
1. Imported module’s code gets executed after interpretation.
2. Only asked programs and variables of imported module are
present in the program.
3. No namespace is created. Import objects of module are
attached to current namespace.
How to Make modules in

Python?
To make a module in
to make a .py
python you have
file which has a name. Suppose
we make a file “Shape.py”.
• Then we write different functions of area within it.
• And we put it within same folder where our program
is stored. This is module.

We used the module in our program by


using import keyword. To use the
Output members of module we use (.) as shown
in the example.
How to Make modules in

Python?
Using from <module> import
<function_name>

This is module.

By using “from shape import AreaCircle


“only AreaCircle function is imported
Output and to call this no need to use (.)
operator.
Namespaces in

Python
We imported a module in to a program which was
referred as a namespace.
• To define a Namespace it is necessary to define its
name.
• Python name is a kind of identifier which we use to
access any python object. And in Python each and
everything is an object.
• Namespaces is used to separate the different
sections of a program.
• Python has 3 types of namespaces -
– Global
– Local
– Built-in
Namespaces in
Python. .
• This is just like variable scope in Python.
• When we start interpreter Then a
namespace is created in which print( ) and
id( ) etc. are already exist. This holds all the
built-in name.
• Each module creates its own global
namespace.
• When we call a function then a local python
namespace is created where all the names
of functions are exist.
Resolving the scope of a
• Forname
every name reference python always
follows name resolution rule when you access
varaibles from any program or function.
• This is known as LEGB rule . Following works
are done when this rule is followed -
– First variable is checkedin Local
Environment, and if it is available then
it will be used.
– Then variable is checked in Enclosing Environment,
and if it is
available then it will be used.
– Then variable is checked in Global Environment, and
if it is available then it will be used.
– Then variable is checked in Built-in Environment, and
if it is available then it will be used.
– Other wise Python will generate an error.
Module Aliasing in

Python
When you import module in Python program then we
have to use the following syntax to make alia name
of module -
import <ModuleName> as
<AliaName>
• Then we use (.) operator to use the members
This is module. of
the module with alias name of module. Example is
given below -
Creating and Using the Alias Name
of Module within a program.

Output
Module Aliasing in

Python
When you import member of any module with alia
name then you have to use the following syntax -
from <ModuleName> import <MemberName> as
<AliaName> This is module.

Creating and using of alias name


of member of module.

Output
PACKAGE /

LIBRARY
Python Package
modules.
is the collection of same type of

• You can use built in package as well as your own


package.
• Python Package is a simple directory. Modules are
stored in that directory.
• Package or library are generally same.
• Steps to make a package is as follows – (geometry)
1. We create a folder (directory) named geometry which will
contain two files area.py and volume.py
2. In the same directory we will put another file named
“__init
.py”
3. init .py is necessary because this is the file
which tells Python that directory is package. And this file
Making PACKAGE /
LIBRARY This is the folder
which will be our
package.

This is init .py file


which is made directly
by saving from
python IDLE
Here the Package is used.
These are the
modules which are
the members of the
package.

Output
Locating The
• When Module
we import a module then
interpreter of python searches the
module in the following sequence –
1. First current directory
2. If module not found then it will be
searched in shell variable PYTHONPATH.
3. If not found anywhere then python
searches in default. Default path is the
path where Python is installed.
Standard Python
• InlastLibraries
chapter we discussed about math and
string module. Same way we will discuss two
more important libraries. - datetime library
or datetime module.
• Python supports yyyy-mm-dd format for date.
It has tow important classes -

– date – time
class class
• today ( ) • now ( )
• year( ) • hour ( )
• month • minute (
() )
• day ( ) • second (
Standard Python
Date Class Libraries Time Class

Passing arguments in Time Class.


THANK YOU

You might also like