Module is a collection of classes and Functions in Python. Functions are grouped into module based on its functionality.
Type
- Built-in
- User Defined
Example – User Defined Module
# Math Module
def add(a,b):
return a+b
def sub(a,b):
return a-b
def prod(a,b):
return a*b
def div(a,b):
return a/b
Save the file as “mathdemo.py”
Program 1
Using the Module
import mathdemo as md
print(“Difference = “, mathdemo.sub(5,3))
print(“Product = “, md.Div(9,3))
Program 2
# calling the module
from mathdemo import *
print(“Sum = “, add(2,3))
print(“Product = “, prod(2,3))
Note: import keyword is used add the module reference to the current Python file.
prefix the module name before the function name.
Win Corporate Training
website: http://www.wincorptrg.com