and allow them to be physically moved into other files,
called modules, that can be included in your main
Python code with the import statement. Creating
modular code provides the following benefits:
Easier readability/maintainability: Code written in a modular
fashion is inherently easier to read and follow. It’s like chapters in a
book providing groupings of similar concepts and topics. Even the best
programmers struggle to understand line after line of code, and
modularity makes maintaining and modifying code much easier.
Low coupling/high cohesion: Modular code should be written in
such a way that modules do not have interdependencies. Each module
should be self-contained so that changes to one module do not affect
other modules or code. In addition, a module should only include
functions and capabilities related to what the module is supposed to do.
When you spread your code around multiple modules, bouncing back
and forth, it is really difficult to follow. This paradigm is called low
coupling/high cohesion modular design.
Code reusability: Modules allow for easy reusability of your code,
which saves you time and makes it possible to share useful code.
Collaboration: You often need to work with others as you build
functional code for an organization. Being able to split up the work and
have different people work on different modules speeds up the code-
production process.
There are a few different ways you can use modules in
Python. The first and easiest way is to use one of the
many modules that are included in the Python standard
library or install one of thousands of third-party modules
by using pip. Much of the functionality you might need
or think of has probably already been written, and using
modules that are already available can save you a lot of
time. Another way to use modules is to build them in the
Python language by simply writing some code in your
editor, giving the file a name, and appending a .py
extension. Using your own custom modules does add a
bit of processing overhead to your application, as Python
is an interpreted language and has to convert your text
into machine-readable instructions on the fly. Finally,
you can program a module in the C language, compile it,
and then add its capabilities to your Python program.
Compared to writing your own modules in Python, this
method results in faster runtime for your code, but it is a
lot more work. Many of the third-party modules and