Page 285 - CTS - CSA TP - Volume 2
P. 285
COMPUTER SOFTWARE APPLICATION - CITS
EXERCISE 130 : Write a python program for importing a
module
Objectives
At the end of this exercise you shall be able to
• develop python program for importing a module.
Procedure
TASK 1: Module contains a utility function
Step1
• Create a module named utilities.py
• Save the following code in a file named utilities.py
• This module contains a utility function
# File: utilities.py
def multiply(a, b):
return a * b
# End of utilities.py
Step 2:
# Now, create a Python program to import and use the module
# File: main_program.py
# Import the module
import utilities
# Use the function from the imported module
result = utilities.multiply(5, 3)
# Display the result
print(f”The result of multiplication is: {result}”)
# End of main_program.py
Output:
In this example, utilities.py is a separate Python file containing a function multiply. The main_program file imports
this module and calls the multiply function to return the product of two numbers . Make sure both files (utilities.
pyandmain_program.py) are in the same directory or provide the correct path when importing the module.
270