module, package, library
module, package, library
functions,
with
Psogram Program to create a module
Program 44: A contains 2
functions
module arith.py
# This
Thismodule name 1s
# add(x, y):
def
ZXcam of tWO nos 2)
print('s
def sub(x, y):
Z= X-y
return Z
function is displaying the
oftwo numbers;
In the preceding observe
whereas,
code, that the add) result
of Sa
the sub() function is returning the result of s1ubtracton.
call
by the name 'arith.py'. Now we can 'arith.py' as a module
this code in a file program, e.g. 'use.py'. For this purpose, We should
arith.py'
be moduleintothe use.py' program. This can be done by writing import statet
used in another
inside the'use.py' program. Thefollowing are different ways of writing import statement
O import
When arith
we write the module name in the import statement as shown in the preceting
module name before every Python object in our
code, PVM add this
will means
That if we have the add() function in our program, PVM wil mak.ie pogram,
arith.add) internally.
use.py. Hence, we should call the add() function in our program
arith.add() or arith.sub().
O import arith as ar
the 'arith' module. When an alias n
Here, 'ar' is another name or alias name for
in the progre
used like this, PVM will add the alias name before the Python object
ar.subl).
ar.add) or
Hence, we can call the function using the alias name as
Pomarithimport
represents all' In this case, PVM
does not bring the
Instead, it brings all the module narme 'arith irit
HereprOgram,
program.
our That means the
names add and names
sub are inside the arith' module int r
can
refer tothose functions
directly available
as add) or directly to our
Hence, we
import add,
arith spccifically
sub sub) progam
fron mentioning the
are names of the
from the functions
O add and
we
Here, PVM brings the nanes sub add and sub. n
case, Therefore, we can call them
directly arith
as add() or module this
program.
In Program45, we have shown how toimport the 'arith.py' module directly into
sub). and call the add) and
our
available in that module.
functions
sub)
call
functions of the module
the(10,
arith.add
# 22)
result = arith.ssub(10, 22)
print('Result of subtraction=, result)
Output:
C:\> python use.p
nos=
twosubtraction=
Sum of of
Result -12
30 Obiects
New Tect Document.btt Desitop New TextDocunnent.bt
Documents
Downlcads
Pictures
Videcs
File nameinitpy
Save as type: Text Decurnents bt)
Cance
A Hide folders Encoding UTF-&
mypack. We can
use
this
mypack mvpack folder. Nor
of
out the package. We can write
the package in any
package, as shown in Program 46. use.Py program toprogram like
stored
mypDack use.py that
imnport the modul
e
the e from
Program showing how to use a
mypackmodulpackage
e belonging to a
46:A
Program
the arith.py module of
import mypack.arith
#using
class2,
TheSpecialVariable name
When aprogramis executed in Python. there is a special variable internaly tetx
executed as name
an individual
variable storesinformation regarding
This program or as a module. When thewhether
program
the name
directly, the Python interpreter stores the value main into this variable. Whn
program isimported as a module into another program, then Python interpreter
the module name into this variable. Thus, by observing the value of the s
the programn is executed.
understand how
can
name__ we When this
written a program.
interpreter
Let'sassumestores
that
have
thewevalue main intothe special variable program
name is Hence,
run,
as a program or not as:
program is run directly
check ifthis ==main:
i f name
code is run as a program')
print('This
Suppose, if_name_variable does not contain the value main_ it representy
this code is run as a module from another external program. To understand this cone
function that displays Some message
write a program with a display()
first we will shown in Program 47.
HelloPython!". This code is