Professional Documents
Culture Documents
--------
Def: Module is the collection of variables, functions, classes,....
Def: Module is a python file contains variables, functions, classes,....
To use modules in python applications we have to use the following two steps.
1. Create Module.
2. Access members from module
1. Create Module:
------------------
In Python applications, Every python file is treated as module,so to create module
we have to create a python file with .py extension.
EX: mymath.py
-------------
a = 10
b = 20
def add():
print(a+b)
def sub():
print(a-b)
test.py
-------
import cal
print("a :",cal.a)
print("b :",cal.b)
cal.add()
cal.sub()
cal.mul()
D:\python10>py test.py
a : 10
b : 5
ADD : 15
SUB : 5
MUL : 50
test.py
---------
from cal import a
from cal import b
from cal import add
from cal import sub
from cal import mul
print("a :",a)
print("b :",b)
add()
sub()
mul()
D:\python10>py test.py
a : 10
b : 5
ADD : 15
SUB : 5
MUL : 50
test.py
--------
from cal import a,b,add,sub,mul
print("a :",a)
print("b :",b)
add()
sub()
mul()
D:\python10>py test.py
a : 10
b : 5
ADD : 15
SUB : 5
MUL : 50
Note: By using single import statement we are able to import all the members of the
specified module by using * notation also.
Syntax: from moduleName import *
EX:
---
cal.py
------
a = 10
b = 5
def add():
print("ADD :",(a+b))
def sub():
print("SUB :",(a-b))
def mul():
print("MUL :",(a*b))
test.py
--------
from cal import *
print("a :",a)
print("b :",b)
add()
sub()
mul()
D:\python10>py test.py
a : 10
b : 5
ADD : 15
SUB : 5
MUL : 50
test.py
---------
import cal as math
print("a :",math.a)
print("b :",math.b)
math.add()
math.sub()
math.mul()
D:\python10>py test.py
a : 10
b : 5
ADD : 15
SUB : 5
MUL : 50
--> In Python applications, we are able to provide alias names to the module
members also.
Syntax: from moduleName import memberName1 as aliasName1, memberName1 as
aliasName2,..
EX:
---
cal.py
------
a = 10
b = 5
def add():
print("ADD :",(a+b))
def sub():
print("SUB :",(a-b))
def mul():
print("MUL :",(a*b))
test.py
-------
from cal import a as x,b as y, add as sum, sub as diff, mul as prod
print("a :",x)
print("b :",y)
sum()
diff()
prod()
Q)Find the valid python import statements from the following list?
-------------------------------------------------------------------
1. from cal ---> Invalid
2. from cal * ---> Invalid
3. import cal ----> Valid
4. import cal * ----> Invalid
5. from cal import * ---> Valid
6. from cal import a ---> valid
7. from cal import a,b,add,sub,mul ----> valid
Ans:
----
To import members from cal module if we use "import cal" syntax then we must use
module name to access members.
To import members gtom cal module if we use "from cal import *" syntax then it is
not required to use module name to access members.
-->In Python applications, when we execute modules , PVM will compile the modules
and PVM will generate a seperate compiled file at the location "ApplicationFolder\
__pycache__\ fileName.cpython-37.pyc
EX:D:\python10\app05\__pycache__\cal.cpython-37.pyc
--> In Python applications, a particular module will be loaded only one time even
though we have provided multiple times import statements.
EX:
---
hello.py
--------
print("Hello, This is from hello module")
test.py
--------
import hello
import hello
import hello
import hello
import hello
print("Hello, This is from test module")
D:\python>py test.py
Hello, This is from hello module
Hello, This is from test module
test.py
--------
from importlib import reload
import hello
print(hello.msg)
print(hello.msg)
print("Application is in Pausing state, perform modifications on hello module")
val = input()
reload(hello)
print(hello.msg)
print(hello.msg)
print(hello.msg)
EX:
---
TicketReservation.py
---------------------
no_Of_Vacancies = 3
customers_And_SeatNos = {}
def bookTocket(seatNo, customerName):
global no_Of_Vacancies
if no_Of_Vacancies > 0:
customers_And_SeatNos[seatNo] = customerName
no_Of_Vacancies = no_Of_Vacancies - 1
print("Hello",customerName, "Your Ticket is confirmed and your seat
no",seatNo)
else:
print("No Tickets are available")
def getStatus():
if no_Of_Vacancies != 0:
print("No of Vacancies :",no_Of_Vacancies)
print("Select Seat number except ",list(customers_And_SeatNos.keys()))
else:
print("All seats are Filled")
test.py
--------
from importlib import reload
import TicketReservation as res
while True:
res.getStatus()
seatNo = int(input("Enter Seat No : "))
customerName = input("Enter Customer Name :")
res.bookTocket(seatNo,customerName)
option = input("Onemore Ticket[yes/no]? :")
if option == "yes":
if(res.no_Of_Vacancies == 0):
break
else:
continue
else:
break