Online Python Libraries
Online Python Libraries
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
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.
Malplotlib library Provides functions and tools to produce quality output in variety of
formats such as plot, charts, graph etc,
12.5.20What 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
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 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
Functions Group of statements
Composition/Structure of python module
MODULES
VARIABLES OTHER
PYTHON
MODULES
FUNCTIONS
VARIABLES
IMPORT
CLASSES
MEMBERS OTHER
PYTHON
METHODS MODULES
Importing Python modules
To import entire module
◼ import
<module name>
◼ Example: import math
VINOD
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.
VINOD
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
VINOD
for
Namespace
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.
for
Namespace
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
name of object as <modulename>.<objectname>
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()
Processing of from module import object
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