COMP-122
INTRODUCTION TO COMPUTER
PROGRAMMING
Lecture 9: Modules
OUTLINE
• Introduction
• Importing modules
• Practice work
What is a Module?
• A module is a .py file that contains Python code.
• Example of a module can be a file named hello.py with
the following code:
print("Hello")
Running the Module in Command
Line
• Here are the steps to follow:
1. Open command shell
2. Navigate to directory where the module is located
3. Input the command
>>> python module_name.py
Then press Enter key
Module Readability
• In modules we use docstrings for readability.
Docstrings are multiline comments that are delimited
by triple
quotes: “““
• They are strategically located at the beginning of
important code sections.
• Their goal is to succinctly describe what the code
section is about.
Module Readability cont…
• Example of docstring in module
"""
This function computes factorial of n.
n! = n* (n-1) * (n-2) *...1
Parameters
----------
n : must be integer
"""
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
Importing Modules
• We have 3 different ways of importing modules in
Python.
1. import module_name
2. from module_name import *
3. from module_name import item_1, item_2,
item_3
Importing Modules cont…(2)
• The first way of importing modules is equivalent of the command we
learned in shell can be written as follows in an IDE:
o import module_name
o import module_name as alias
• Here, you have permission to use everything in the module by its name.
But you must use its module_name and that involves using the “dot
notation.” i.e.
o module_name.item_name
Importing Modules cont…(3)
• Example code:
import math
x = math.sqrt(81)
print(f'Square root of 81 is {x}')
Importing Modules cont…(4)
• The second way of importing modules we use the following code:
o from module_name import *
• Just like the previous code, you have permission to use everything
in the module. This time you use the code items from the module
directly, you do not use the module_name and the “dot
notation.”
o item_name
Importing Modules cont…(5)
• Example code:
from math import *
radius = 5.0
area = pi*radius**2
print(area)
Importing Modules cont…(6)
• The final way of importing modules, we use the following code:
o from module_name import item_1, item_2, item_3
• In this case, you have permission to use everything only the
explicitly listed items from the module. These items from the
module are accessed directly. For example,
o item_2
Importing Modules cont…(7)
• Example code:
from math import pi, sqrt
diameter = 32
print("circumference is", pi*diameter)
print("area is", pi*(diameter/2)**2)
Practice Work
Module name: area
Module description: Computes area of a shape
Module functions:
square =>
circle =>
triangle =>
trapezium =>
rectangle =>
Practice Work cont…(2)
Using previous details:
1 Create a custom module
A Add comments
B Add docstrings
2 Import the module using
A import module_name
B from module_name import *
C from module_name import item_1, item_2, ...
3 Use dir() function to see module contents
eg. dir(math) will lists everything in math library
4 Use help() function to get documentation
eg. help(math) prints math library documentation