0% found this document useful (0 votes)
2 views55 pages

Using Python Libraries2r

The document provides an overview of using Python libraries and modules, emphasizing the importance of organizing code into modules for better management of complex programs. It explains the relationship between modules, packages, and libraries, and outlines common Python libraries such as math, random, and NumPy. Additionally, it covers importing modules, creating custom modules, and the structure of Python modules, including the use of namespaces and the init.py file for package creation.

Uploaded by

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

Using Python Libraries2r

The document provides an overview of using Python libraries and modules, emphasizing the importance of organizing code into modules for better management of complex programs. It explains the relationship between modules, packages, and libraries, and outlines common Python libraries such as math, random, and NumPy. Additionally, it covers importing modules, creating custom modules, and the structure of Python modules, including the use of namespaces and the init.py file for package creation.

Uploaded by

Iramfatima
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

for more details visit:

[Link]
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

USING PYTHON LIBRARIES


COLLECTION OF MODULES
for more details visit:
[Link]

Introducti
on
 As our program become larger and more
complex the need to organize our code
becomes greater. We have already learnt in
Function chapter that large and complex program
should be divided into functions that perform a
specific task. As we write more and more
functions in a program, we should consider organizing of
functions by storing them in modules
 A module is simply a file that contains
Python code. When we break a program into
modules, each modules should contain functions
that perform related tasks.
 Commonly used modules that contains source
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Introducti
oncode for generic needs are called Libraries.

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Introducti
on
 When we speak about working with
libraries in Python, we are, in fact,
working with modules that are created
inside Library or Packages. Thus a Python
program comprises three main components:
🞑 Library or Package
🞑 Module

🞑 Function/Sub-routine

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:

Relationship between Module, [Link]

Package
and Library in Python
 A Module is a file containing Python
definitions (docstrings) , functions,
variables, classes and statements
 Python package is simply a directory of
Python module(s)
 Library is a collection of various
packages. Conceptually there is no
difference between package and Python
library. In Python a library is used to
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
loosely describe a collection of core or main
[Link]

modules

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Commonly used Python libraries


STANDARD LIBRARY
math module Provides mathematical functions
cmath module Provides function for complex numbers
random module For generating random numbers
Statistics module Functions for statistical operation
Urllib Provides URL handling functions so that you can access websites
from within your program.
NumPy library This library provides some advance math functionalities
along with tools to create and manipulate numeric arrays
SciPy library Another useful library that offers algorithmic and
mathematical tools
for scientific calculation
Tkinter library Provides traditional user interface toolkit and helps you
to create user friendly GUI interface for different types
of applications.
Matplotlib Provides functions and tools to produce quality output in
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

What is module?
 Act of partitioning a program into
individual components(modules) is called
modularity. A module is a separate unit in
itself.
🞑 It reduces its complexity to some degree
🞑 It creates numbers of well-defined, documented
boundaries within program.
🞑 Its contents can be reused in other program,
without having to rewrite or recreate them.

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Structure of Python module


 A python module is simply a normal python
file(.py) and contains functions, constants and
other elements.
 Python module may contains following objects:
docstring Triple quoted comments. Useful for documentation
purpose
Variables and For storing values
constants
Classes To create blueprint of any object
Objects Object is an instance of class. It represent class in real
world
Statements Instruction
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]
Functions Group of statements

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Composition/Structure of python module

MODULES

VARIABLES OTHER
PYTHON
MODULES
FUNCTIONS

VARIABLES
IMPORT
CLASSES

MEMBERS OTHER
PYTHON
METHODS MODULES

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Importing Python modules


 To import entire module
 import
<module name>
 Example: import math

 To import specific function/object from module:


 from<module_name> import <function_name>
 Example: from math import sqrt

 import * : can be used to import all names


from module into current calling module

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Accessing function/constant of imported module

 To use function/constant/variable of
imported module we have to specify
module name and function name
separated by dot(.). This format is known
as dot notation.
 <module_name>.<function_name>
 Example: print([Link](25))

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Example : import module_name

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Example: from module import


function
 By this method only particular method will
be added to our current program. We
need not to qualify name of method with
name of module. Or
example:
Here function
sqrt() is
directly written

This line will


SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
not be
[Link]
executed and
gives an
error

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &

SACHIN BHARDWAJ, PGT(CS), KV NO.1


TEZPUR
for more details visit:
[Link]

Example: from module import *


 It is similar to importing the entire
package as “import package” but by this
method qualifying each function with
module name is not required.

We can also import multiple elements of module as :


from math import sqrt, log10
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Creating our own Module


 Create new python file(.py) and type the
following code as:
Execute the following code to import
and use your own module

Save this file are “[Link]”


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

help() function
 Is used to get detailed information about
any module like : name of module,
functions inside module, variables inside
module and name of file etc.

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Namespac
e
 Is a space that holds a bunch of names.
Consider an example:
🞑 In a CCA competition of vidyalaya, there are
students from different classes having similar
names, say there are three POOJA GUPTA, one
from class X, one from XI and one from XII
🞑 As long as they are in their class there is no
confusion, since in X there is only one POOJA GUPTA,
and same with XI and XII
🞑 But problem arises when the students from X,
XI, XII are sitting together, now calling just
POOJA GUPTA would create confusion-which class‟s
POOJA GUPTA. So one need to qualify the name as
class X‟s POOJA GUPTA, or XI‟s or XII‟s and so on.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Namespac
e
 From the previous example, we can say that class X
has its own namespace where there no two names
as POOJA GUPTA; same holds for XI and XII
 In Python terms, namespace can be thought of as a
named environment holding logical group of related
objects.
 For every python module(.py), Python creates a
namespace having its name similar to that of
module‟s name. That is, namespace of module AREA
is also AREA.
 When 2 namespace comes together, to resolve any kind
of object name dispute, Python asks you to qualify the
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Namespac
e name of object as <modulename>.<objectname>

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Processing of import <module>


 The code of import module is
interpreted and executed
 Defined functions and variables in the
module are now available to program in
new namespace created by the name of
module
 For example, if the imported module is area,
now you want to call the function area_circle(),
it would be called as area.area_circle()

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Processing of from module import object


 When we issue from module import object command:
 The code of imported module is interpreted and executed
 Only the asked function and variables from module are
now available in the current namespace i.e. no new
namespace is created that’s why we can call object of
imported module without qualifying the module name
 For example:
from math import sqrt
print(sqrt(25))
 However if the same function name is available in current
namespace then local function will hide the imported module’s
function
 Same will be apply for from math import * method

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Creating
Package
Step 1
 Create
a new folder which you want to act as
package. The name of folder will be the name of

IN THE C:\USERS\VIN
A new Folder “mypackage”
is created.
Note: you can create folder in
any desired location
your package
for more details visit:
[Link]

Creating
Package
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more details visit:
[Link]

Creating
Package
 Step 2: Create modules (.py) and save it in
“mypackage” folder [Link]

[Link]

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


&
for more details visit:
[Link]

Creating
Package
 Step 2: importing package and modules in
python program

Save this file by


“[Link]”
outside the
package folder

RUN THE PROGRAM


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Creating Alias of
Package/module
 Alias is the another name for imported
package/module. It can be used to shorten the
package/module name

Save this file by


“[Link]”
outside the
package folder
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1 RUN THE PROGRAM
TEZPUR
for more details visit:
[Link]

init .py file


 init .py (double underscore is prefixed and
suffixed) : This file is required to make Python treat
directories containing the file as packages.
 init .py can be just empty file, but it can also
execute initialization code for the package.
 Note: Python 2 requires init .py to be inside a folder
in order for the folder to be considered a package and
made importable but in Python 3.3 and above, it support
implicit namespace packages, all folders are packages
regardless of the presence of a init .py file
 So from Python 3.3 it is optional to create init .py file
for more details visit:

Example - creating and using [Link]

package
and module and init .py
 Create a folder to act as a package
 For. e.g. In C: (C Drive) a folder
“mypackages” is created
 Create init .py file (do not write any thing
in it) in this folder “mypackages”
 Now create another folder “package1”
inside “mypackages”
 Create init .py file inside “package1” also
 Create a module(.py) for e.g. “[Link]”
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
file in “package1” to have some functions [Link]

it.

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:

Another example of creating and [Link]

using
package and module and init
.py
 [Link]

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:

Another example of creating and [Link]

using
package and module and init
.py
 Now our entire contents will be like this:
Here,
mypackages
and
package1
are folders

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
 Now in C: (C Drive), create a file for e.g. [Link]

“[Link]” in which we will import module


“mymodule”

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:

Another example of creating and [Link]

using
package and module and init
.py
 To import the packages, we can either use
Absolute addressing or Relative Addressing.
(already discussed in data file handling chapter)
 Absolute means following the complete address
whereas Relative means with relation to current
folder by using Single dot(.)

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Absolut
e path
of
packag
e

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:

Another example of creating and [Link]

using
package and module and init
.py
 Now our entire content structure will be as:

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
 Run the [Link] file and you will get the output.
[Link]

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

TIP : to use package from any


location
 At this time, the package “mypackages” will be
accessible from its location only i.e. the file which
wants to import this package must be in same
folder/drive where “mypackages” is.
 To enable “mypackages” to be used from any
location, Copy this mypackages to Python‟s site-
packages folder inside Lib folder of Python‟s
installation folder. (Try with copying to Lib folder also)
 Path of site-packages is :
C:\Users\vin\AppData\Local\Programs\Python\
Python 36-32\Lib\site-packages
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
 After this you can import this package [Link]

“mypackages” in any python file.

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Using Python‟s Built-in Function


 Python‟s standard library is very extensive
that offers many built-in functions that we
can use without having to import any library.
 Using Python‟s Built-in functions
 Function_name()

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Mathematical and String


functions
 oct(int) : return octal string for given number by prefixing
“0o”
 hex(int) : return octal string for given number by prefixing
“0x”

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Mathematical and String


functions
 int(number) : function convert the fractional
number to integer
 int(string) : convert the given string to integer
 round(number,[nDIGIT]) : return number
rounded to nDIGIT after decimal points. If
nDIGIT is not given, it returns nearest integer
to its input.
 Examples: (next slide)

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Mathematical and String


functions

SACHIN BHARDWAJ, PGT(CS), KV NO.1


TEZPUR
for more details visit:
[Link]

Mathematical and String


functions
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &

SACHIN BHARDWAJ, PGT(CS), KV NO.1


TEZPUR
for more details visit:
[Link]

Other String
function
 We have already used many string function
in class XI, here are few new functions
 <string>.join(): if the string based iterator is a
string then the <string> is inserted after every
character of the string.
 If the string based iterator is a list or tuple of
strings then, the given string/character is joined
after each member of the list of tuple. BUT the tuple
or list must have all members as string otherwise Python
will raise an error
 Examples (next slide)
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Other String
function

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Other String
function
 We have already used many string function
in class XI, here are few new functions
: allow to divide string in multiple
 <string>.split()
parts and store it as a LIST. If you do not provide
delimeter then by default string will be split
using space otherwise using given character.

 <str>.replace(): allows you to replace any part of string


with another string.

 Example (NEXT SLIDE)


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Example (split() and replace())

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Using URLLIB and WEBBROWSER modules


 Python provides urllib module to send http
request and receive the result from within
your program. To use urllib we have to first
import urllib module.
 urllib module is a collection of sub-
module like request, error, parse etc.
following functions of urllib we can use:
(next slide)

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Functions of URLLIB
FUNCTION NAME PURPOSE
[Link](<url>) Opens a website or network
object denoted by URL for
reading and return file like object
using which other functions are
often used
urlopen_object.read() Return HTML or the source code of
given url
urlopen_object..getcode() Returns HTTP status code where 200
means „all okay‟ 404 means url not
found etc. 301, 302 means some
kind of redirections happened
urlopen_object.headers Stores metadata about the open URL

urlopen_object.info() Returns same information as by


headers
urlopen_object.geturl()
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR
Return URL string
& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

Example:

Before execution of
the above code, make
sure PC is connected
to working internet.

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR


& SACHIN BHARDWAJ, PGT(CS), KV NO.1
TEZPUR
for more details visit:
[Link]

WEBBROWSER MODULE
 Provides functionality to open a website in a
window or tab or web browser on your computer,
from within your program.
 To use webbrowser module we must import the module as:
🞑import webbrowser

VINOD KUMARBHARDWAJ,
SACHIN VERMA, PGT(CS), KVKV
PGT(CS), OEF KANPUR &
NO.1
TEZPUR

You might also like