Chapter (9) Modules

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Chapter (9)

Modules
What is a Module?

Modules are used to categorize code in Python into smaller part. A module is simply a file, where
classes, functions and variables are defined. Grouping similar code into a single file makes it easy
to access.

Advantages for using module

1) Reusability: Module can be used in some other python code. Hence it provides the facility of code
reusability.

2) Categorization: Similar type of attributes can be placed in one module.

Importing a Module

There are different ways by which you we can import a module. These are as follows:

1. Using import statement


2. Using from … import statement
3. Import whole module

1) Using import statement

Create the “Module_DrawStar.py”

def Fun_DrawStar():

for r in range(0,5):

str="";

for c in range(0,r+1):

str+="*\t"

print (str)

Create the “DrawStar.py”

import Module_DrawStar

Module_DrawStar.Fun_DrawStar()

2) Using from … import statement

Create the “Module_Shape.py”

def Circle(radius):

area=3.14159*radius*radius

Python Programming Ch9-1


KMD Computer Centre
print ("Circle Area = " , area)

def Square(size):

area=size*size

print ("Square Area = ", area)

Create the “Shape.py”

from Module_Shape import Circle,Square

Circle(10)

Square(5)

3) Import whole module

Create the “Module_Company.py”

def Centre1():

print ("KMD - PSD")

print ("No 174-182, Pansodan Road, Kyauktada")

print ("381776, 381035\n")

def Centre2():

print ("KMD - MNG")

print ("No 331, Pyay Road, Myaynigone")

print ("534170, 502233\n")

def Centre3():

print ("KMD - China Town")

print ("No 90, Hledan Street, Lanmadaw")

print ("0951220555\n")

Python Programming Ch 9-2


KMD Computer Centre
Create the “Company.py”

from Module_Company import *

Centre1()

Centre2()

Centre3()

Python Programming Ch 9-3

You might also like