0% found this document useful (0 votes)
19 views

Online Python Libraries

Here are the steps to complete the homework: 1. Create a module called temp.py with the following functions: - def c_to_f(c): #function to convert celsius to fahrenheit - def f_to_c(f): #function to convert fahrenheit to celsius 2. Create a module called num.py with the following functions: - def odd_even(n): #function to check if a number is odd or even - def reverse_num(n): #function to reverse a number 3. Import and test the functions from these modules in a separate python file. For example: from temp import c_to_f,

Uploaded by

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

Online Python Libraries

Here are the steps to complete the homework: 1. Create a module called temp.py with the following functions: - def c_to_f(c): #function to convert celsius to fahrenheit - def f_to_c(f): #function to convert fahrenheit to celsius 2. Create a module called num.py with the following functions: - def odd_even(n): #function to check if a number is odd or even - def reverse_num(n): #function to reverse a number 3. Import and test the functions from these modules in a separate python file. For example: from temp import c_to_f,

Uploaded by

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

USING PYTHON LIBRARIES

 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.
Introduction
 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
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

 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
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(math.sqrt(25))
Example : import module_name
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 not


be executed and
gives an error

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.

We can also import multiple elements of module as :


from math import sqrt, log10

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

Save this file are “area.py”


VINOD
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
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

 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
Creating Package
 Step 1
◼ Create a new folder which you want to act as package. The
name of folder will be the name of your package

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

Save this file by


“anyname.py” outside
the package folder

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

Save this file by


“anyname.py” outside
the package folder

RUN THE PROGRAM


Home work
1.CREATE A MODULE AS TEMP.PY
A.CONVERT DEGREE CEL TO FAHRENHEIT
AND VICE VERSE
2.CREATE A MODULE AS NUM.PY WITH FOLLOWING FUNCTIONS
A.TO CHECK ODD OR EVEN
B.TO REVERSE NO
C TO CHECK IF NO IS PALINDROM NO

CREATE A PACKAGE WITH NAME MYPACKAGE


15-5-20
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()
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”
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)
Mathematical and String functions
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
Other String function
 We have already used many string function in class
XI, here are few new functions
◼ <string>.split() : allow to divide string in multiple 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 (split() and replace())


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:
Functions of URLLIB
FUNCTION NAME PURPOSE
urllib.request.urlopen(<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() Return URL string


Example:

Before execution of the


above code, make sure PC
is connected to working
internet.
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

You might also like