Using Python Libraries
Using Python Libraries
PRESENTATION.
PDF Format
CHAPTER 4
Modules
Packages
libraries
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.
Module is a collection
of functions, classes,
constants and other
statements.
Write the code on the board
in a file and save it with a
name with .py suffix.
It becomes a module.
Can you try your Yes 2 . I want to write
own modules ? some more module
files.
Collection of modules
saved in a folder, are
called package.
Library is a collection of packages.
Suppose we write more packages about
anything. It will become a library. In
general, the terms package and library
have the same meaning.
PYTHON has rich libraries.
,
.
The Python Standard Library contains
built-in data, functions, and many modules.
All are part of the Python installation.
Basic Contents of Standard Library.
and
Built-in
Data,
functions
+ more
Module to import
1. Math module
2. Cmath module
3. Random module
4. Statistics module
5. Urllib module
print ( "10 + 20 = ", 10 + 20 ) print(), input(),
10 + 20 are basic operation.
print ( "Cube of 2 is ", 2 ** 3 ) No need to import anything.
sqrt () and pow () are defined in
Math module. So need to
import math import math modules.
We use the
Statement to import other
modules into our programs.
Examples of Built-in Function.
>>> ‘zebra’.replace(‘ze’,’co’)
cobra zebra becomes cobra
>>> ‘cobra’.replace(‘co’,’ze’)
zebra cobra becomes zebra
Modules such as math, cmath, random, statistics, urllib are
not an integral part of Python Interpreter. But they are
part of the Python installation. If we need them,
import them into our program and use them.
.
class XI-B Abhijit .
class XII-A Abhijit
Are not Ashwin in 12.B?
Your Reg. No 675323.
No Sir, Check
the list of 12.A
An object (variable, functions
Google K.M. Abraham. etc.) defined in a namespace is
associated with that
Who is കുഴിക്കാലായിൽ namespace. This way, the
അബ്രഹാാം(K M Abraham) same identifier can be defined
He is a member of in multiple namespaces.
കുഴിക്കാലായിൽ Object in that
Name of
family. Namespace Namespace
>>> math .
Output : 5
>>> math.pi
Output :3.141592653589793
math
math and cmath modules covers many mathematical
functions.
and .
More than 45 functions and 5 constants are defined in it.
The use of these functions can be understood by their
name itself.
1. import math
2. Place (Namespace) before function name.
Name of Object in that
Namespace Namespace
X = sqrt(25)
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None,
'__loader__': <class '_frozen_importlib.BuiltinImporter'>,
'__spec__': None, '__annotations__': {}, '__builtins__': <module
'builtins' (built-in)>}
>>> a , b = math.modf(math.pi)
a= 0.14159265358979312, b = 3
Statistics module provides 23 statistical functions. Some of
them are discussed here.
Before using them. And call with “statistics.” prefix.
mean() calculate
Arithmetic mean (average) of data.
Output : 2
>>> statistics.mode(“MAVELIKARA")
Output : ‘A'
import random FUNCTIONS.
import webbrowser
url = "https://www.facebook.com/"
webbrowser.open(url) opens mail
webbrowser.open("https://mail.google.com/")
opens youtube
webbrowser.open(“https://www.youtube.com/”)
>>> import webbrowser OUTPUT OF 2 LINE CODE
>>> webbrowser.open("https://www.slideshare.net/venugopalavarmaraja/")
Give any address (URL)here.
.
urllib is a package for working with URLs. It
contains several modules. Most important one is
'request'. So import urllib.request when working
with URLs. Some of them mentioning below..
Open a website denoted by URL for reading. It will return
Urllib.request.urlopen() a file like object called QUrl. It is used for calling following
functions.
returns html or source code of the given url opened via
QUrl.read()
Urlopen().
Returns the http status code like 1xx,2xx ... 5xx. 2xx(200)
QUrl.getcode()
denotes success and others have their meaning.
QUrl.geturl ()
Returns the url string.
import urllib.request
import webbrowser
u = "https://www.slideshare.net/venugopalavarmaraja/"
weburl = urllib.request.urlopen(u) Creating QUrl object(HTTP Request
html = weburl.read() Object)
code = weburl.getcode()
url = weburl.geturl() Calling Functions with QUrl Object
hd = weburl.headers
inf = weburl.info()
print("The URL is ", url)
print("HTTP status code = ", code )
Printing Data
print("Header returned ", hd )
print("The info() returned ", inf )
print("Now opening the url",url ) Opening Website
webbrowser.open_new(url)
So far we have learned What are modules,
packages, and libraries? And familiarized
with Standard Library. Libraries like Numpy,
Scipy, Tkinter, Matplotlib are not part of
Python Standard Libraries. So they
want to download and install. We
do It when we study 8th chapter.
Now we are going to learn
A module is a Python
file that contains a
collection of
functions, classes,
constants, and other
statements.
Creating Module
This is the file I wrote and saved.
File name is MYMOD.py.
What to do next.
Example
.
class XI-B Abhijit .
class XII-A Abhijit
Example 2
Example 3
Import into a
new program.
Structure of a Module
DocString (Documentation String)
Functions
Constants/Variables
Classes
Objects
Other Statements
def fibi(a, b, n) :
print ("Fibonacci Series is ", end="")
for x in range(n):
print (a+b, end=", ")
a, b = b, a+b
def fibo(a, b, n) :
'''fibi () takes 3 arguments and generate Fibonacci series
„a‟ and „b‟ are previous 2 elements. N is the number of
elements we want.'''
print (" Fibonacci Series is ", end="")
for x in range(n):
print (a+b, end=", ")
a, b = b, a+b
All
Calling help of fibo function.
Module_01.py
Module_02.py
Import modules
from them and
__init__.py use as you need.
Create Module_02.py
Create __init__.py
Step 1.
Create Module_01.py
Create Module_02.py
Create __init__.py
Getting Current Working
Directory(folder)
Creating Folfer.
Step 2.
Create Modules. Let us try the
previous module /
import os file MYMOD.py.
os.mkdir(“MY_PACKAGE”)
Create Module_01.py
Create Module_02.py
Create __init__.py
MY >>> import os
Package >>> os.mkdir(“my_package”)
Creating MYMOD.py
MYMOD.py
SAVE IN
MY_PACKAGE Creating
CONVERSION.py.py
MYMOD.py
CONVESION.py
SAVE IN
MY_PACKAGE
Creating MY_MATH.py
Create __init__.py
__init__.py "Underscore underscore init
How is it underscore underscore dot py"
pronounced? Double
Underscore
”Dunder” means Double Underscore.
two leading and two trailing underscores
__init__.py file marks folders
(directories) as Python package.
This will execute automatically
when the package is imported.
The __init__.py file initialize the
Package as its name implies.
Etc..
Step 3. Create __init__.py
import os
os.mkdir(“MY_PACKAGE”)
MYMOD.py
CONVERSION.py
MY_MATH.py
Creating
__init__.py
Step 3. Create __init__.py
MYMOD.py
CONVERSION.py
MY_MATH.py
Creating
__init__.py
Step 3. Empty/Missing __init__.py
Step 3. Create __init__.py
MYMOD.py
CONVERSION.py
MY_MATH.py
Creating
__init__.py
Import into current project & use
‘Pi' is an object
defined in the math module. Partial
import is not possible with the "Import"
statement.
Multiple Modules can be imported with one import
statement.
import my_package.MYMOD,my_package.MY_MATH
But it is not a
standard style.
from module import component is another way
for importing modules or packages. Selected
components can be imported using this statement,
but it is not possible with the import statement.
pi (pi = 3.1415) is a constant defined in math
module. You can’t import partial components.
Only the required objects are imported. These are imported into the
local namespace so no "namespace dot" notation is required.
Output : 3.14…
Only the required objects are imported. These are imported into the
local namespace so no "namespace dot" notation is required.
It is not
possible with
“import”
Statement.
python will
support unicode
malayalam.
The file name will not change, but will
get a nickname. Look at the red square.
The wild card(character) is a
letter (asterisk *) used in the
sense of “all functions and
objects”.
Output : 3
This initiative will make sense if you
find this presentation useful.
If you find any mistakes please
comment.
Namasthe