Importing modules
Python code in one module can get access to the Python code in another module by a process called importing modules.
To elaborate on the different module and package concepts, we will build two modules and one main script that will use those two modules. These two modules will be updated or reused throughout this chapter.
To create a new module, we will create a .py
file with the name of the module. We will create a mycalculator.py
file with two functions: add
and subtract
. The add
function computes the sum of the two numbers provided to the function as arguments and returns the computed value. The subtract
function computes the difference between the two numbers provided to the function as arguments and returns the computed value.
A code snippet of mycalculator.py
is shown next:
# mycalculator.py with add and subtract functions def add(x, y): """This function adds two numbers""" ...