Using Python Libraries-1
Using Python Libraries-1
Using Python Libraries-1
LIBRARIES
COLLECTION OF
Modularizatio
n of python
program
Frame-
work
Libr- Libr-
ary1 ary2
Framework=multiple library
mod-
ule1
mod-
ule2
Library=multiple packages
Package=multiple module
Module=multiple function/class
Using Python Libraries
Following terms must be clear while developing any python
project/program.
1. Module
2. Package
3. Library
4. Framework
1. Using Module -It is a file which contains python functions/global
variables/clases etc. It is just .py file which has python executable code /
statement.For example: Let’s create a file usermodule.py
def hello_message(user_name):
return “Hello " + name
Now we can import usermodule.py module either in python interpreter or
other py file.
import usermodule
print usermodule.hello_message(“India")
Using Python Libraries
How to import modules in Python?
Python module can be accessed in any of following way.
1.Python import statement
import math
print(“2 to the power 3 is ",
math.pow(2,3))
Just similar to math ,user
defined module can be
accessed using import
statement
2.Import with renaming
import math as mt
print(“2 to the power 3
is ", mt.pow(2,3))
3.Python from...import statement
print(“2
from math to the power
import pow3 is ", pow(2,3))
print(“2 to the power 3 is ",
Introductio
n
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 code for
generic needs are called Libraries.
Introductio
n
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
Relationship between Module,
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 loosely describe a collection of core or
main modules
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.
Structure of Python
module
A python module is simply a normal python
file(.py) and contains functions, constants and
other elements.
Python
docstring module may
Triple contains
quoted following
comments. Useful objects:
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
Functions Group of statements
Composition/Structure of python
module
MODUL
ES
VARIAB OTHER
LES PYTHON
FUNCTION MODUL
S ES
VARIAB IMPOR
LES T
CLASS
ES
MEMBE OTHER
RS PYTHON
METHODS
MODUL
ES
Importing Python
modules
To import entire module
import<module name>
Example: import math
namespace.
Have different modules on a single directory with
IN THE C:\USERS\VIN
A new Folder “mypackage”
is created.
Note: you can create folder
in
any desired location
Creating Package
Step 2: Create modules (.py) and save it in
“mypackage” folder numcheck.py
area.py
Creating Package
Step 2: importing package and modules in python
program
RUN THE
PROGRAM
Creating Alias of Package/module
Alias is the another name for imported package/module.
It can be used to shorten the package/module name
RUN THE
PROGRAM
Types of Modules
There are various in-built module in python, we
will discuss few of them
Math module
Random module
Statistical module
Math
module
This module provides various function to
perform arithmetic operations.
Example of functions in math modules are:
sqrt ceil floor pow
fabs sin cos tan
Example of variables in math modules
are:
pi
e
Math module
functions
sqrt(x) : this function returns the square root
number(x)
of module name is
required before
. function name
here
pow(x,y) : this function returns the
(x)y module name is not
required before
function name here
P
L
E
O
T
P
T
Just a
Minute…
Give the following python code, which is repeated
four times. What could be the possible set of
output(s) out of four sets (ddd is any combination
of digits)
import random
print(15 + random.random()*5)
a) b) c) d)
17.ddd 15.ddd 14.ddd 15.ddd
19.ddd 17.ddd 16.ddd 15.ddd
20.ddd 19.ddd 18.ddd 15.ddd
15.ddd 18.ddd 20.ddd 15.ddd
Just a
Minute…
What could be the minimum possible and
maximum possible numbers by following code
import random
print(random.randint(3,10)-
3)
In a school fest, three randomly chosen students out
of 100 students (having roll number 1 -100) have
to present the bouquet to the guests. Help the
school authorities choose three students randomly
Just a
Minute…
Just a
Minute…
Look at the following Python code and find the possible output(s) from the
options (i) to (iv) following it. Also, write the maximum and the minimum values
that can be assigned to the variable PICKER.
Note:
‐ Assume all the required header files are already being included in the code.
‐The function randint() generates an integer between 1 to n
import random
PICKER=1+random.randint(0,2)
COLOR=[”BLUE”,”PINK”,”GREEN”,”RED”]
for I in range(1,PICKER+1):
for j in range(I+1):
print(COLOR[j],end=‘’)
print()
What are the possible outcome(s)
executed from the following code?
Also specify the maximum and
minimum values that can be
assigned to1)DELHIDELHI
variable PICK
2)
DELHI
MUMBAIMUMBAI DELHIMUMBAI
CHENNAICHENNAI DELHIMUMBAICHENN
AI
KOLKATAKOLKAT
A
3) 4)
DELHI DELHI
MUMBAI DELHIMUMBAI
CHENNAI KOLKATAKOLKATAKOLKATA
KOKLATA
randrange()
function
This function is also used to generate
random number within given range.
Syntax
randrange(start,stop,step)
It will generate random
number between 5 to
14
Here, 10 occurs
most in the list.