Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Chapter 3 Using Python Librarieseng 1

This document discusses Python libraries, modules, and packages. It explains that libraries are collections of packages, packages are directories containing modules, and modules are .py files containing code like functions, classes, and variables. The document provides examples of creating modules and packages, importing them, and using module and package functionality. It also discusses Python's built-in datetime module for working with dates and times.

Uploaded by

gunjasrivas1983
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Chapter 3 Using Python Librarieseng 1

This document discusses Python libraries, modules, and packages. It explains that libraries are collections of packages, packages are directories containing modules, and modules are .py files containing code like functions, classes, and variables. The document provides examples of creating modules and packages, importing them, and using module and package functionality. It also discusses Python's built-in datetime module for working with dates and times.

Uploaded by

gunjasrivas1983
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Using Python Libraries

Based on CBSE Curriculum


Class-12

By: Neha Tyagi,


PGT CS
KV No-5, 2nd Shift,
Jaipur

Neha Tyagi, KV No-5 Jaipur


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 3 different
components. -
– Library or package
– Module
– Functions/sub-modules

Neha Tyagi, KV No-5 Jaipur


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
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. Neha Tyagi, KV No-5 Jaipur
Module in Python
• A 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.
– Categorization : same attributes can be stored in one module.

Neha Tyagi, KV No-5 Jaipur


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 of
module.
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.

Neha Tyagi, KV No-5 Jaipur


How to Make modules in Python?
• To make a module in python you have to make a .py 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.
Neha Tyagi, KV No-5 Jaipur
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 there is no need to use
(.) operator.

Neha Tyagi, KV No-5 Jaipur


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

Neha Tyagi, KV No-5 Jaipur


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.

Neha Tyagi, KV No-5 Jaipur


Resolving the scope of a name
• For 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 checked in 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.

Neha Tyagi, KV No-5 Jaipur


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 of the module
with alia name of module. Example is given below -
This is module.

Creating and Using the Alia Name of


Module with in a program.

Output

Neha Tyagi, KV No-5 Jaipur


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 alia name of


member of module.

Output

Neha Tyagi, KV No-5 Jaipur


PACKAGE / LIBRARY
• Python Package is the collection of same type of modules.
• 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 initializes the package
4. Then we import the package and use the content.

Neha Tyagi, KV No-5 Jaipur


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

Neha Tyagi, KV No-5 Jaipur


Locating The Module
• When 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 PYTHONPAT.
3. If not found anywhere then python searches in
default. Default path is the path where Python
is installed.

Neha Tyagi, KV No-5 Jaipur


Standard Python Libraries
• In last chapter we discussed about math and string
module. Same way we will discuss two more
important libraries. - datetime library या datetime
module.
• Python supports yyyy-mm-dd format for date. It has
tow important classes -

– date class – time class


• today ( ) • now ( )
• year( ) • hour ( )
• month ( ) • minute ( )
• day ( ) • second ( )

Neha Tyagi, KV No-5 Jaipur


Standard Python Libraries
Date Class Time Class

Passing arguments in Time Class.

Neha Tyagi, KV No-5 Jaipur


Thank you
Please follow us on

www.pythontrends.wordpress.com

Neha Tyagi, KV No-5 Jaipur

You might also like