Page 445 - CITS - Computer Software Application -TT
P. 445

COMPUTER SOFTWARE APPLICATION - CITS




           Simplification: A module often concentrates on one comparatively small area of the overall problem instead of
           the full task. We will have a more manageable design problem to think about if we are only concentrating on one
           module. Program development is now simpler and much less vulnerable to mistakes.
           Flexibility: Modules are frequently used to establish conceptual separations between various problem areas. It is
           less likely that changes to one module would influence other portions of the program if modules are constructed
           in a fashion that reduces interconnectedness. (We might even be capable of editing a module despite being
           familiar with the program beyond it.) It increases the likelihood that a group of numerous developers will be able
           to collaborate on a big project.
           Reusability:  Functions  created  in  a  particular  module  may  be  readily  accessed  by  different  sections  of  the
           assignment (through a suitably established api). As a result, duplicate code is no longer necessary.

           Scope: Modules often declare a distinct namespace to prevent identifier clashes in various parts of a program.
           In Python, modularization of the code is encouraged through the use of functions, modules, and packages.
           What are Modules in Python?
           A document with definitions of functions and various statements written in Python is called a Python module.

           In Python, we can define a module in one of 3 ways:
           •  Python itself allows for the creation of modules.
           •  Similar to the re (regular expression) module, a module can be primarily written in C programming language
              and then dynamically inserted at run-time.
           •  A built-in module, such as the itertools module, is inherently included in the interpreter.
           A module is a file containing Python code, definitions of functions, statements, or classes. An example_module.
           py file is a module we will create and whose name is example_module.

           We employ modules to divide complicated programs into smaller, more understandable pieces. Modules also
           allow for the reuse of code.
           Rather than duplicating their definitions into several applications, we may define our most frequently used functions
           in a separate module and then import the complete module.
           Let’s construct a module. Save the file as example_module.py after entering the following.
           Example:
           1  # Here, we are creating a simple Python program to show how to create a module.

           2  # defining a function in the module to reuse it
           3  def square( number ):
           4  # here, the above function will square the number passed as the input
           5  result = number ** 2
           6  return result     # here, we are returning the result of the function

           Here, a module called example_module contains the definition of the function square(). The function returns the
           square of a given number.
           How to Import Modules in Python?

           In Python, we may import functions from one module into our program, or as we say into, another module.
           For this, we make use of the import Python keyword. In the Python window, we add the next to import keyword,
           the name of the module we need to import. We will import the module we defined earlier example_module.
           Syntax:

           1  import example_module
           The functions that we defined in the example_module are not immediately imported into the present program.
           Only the name of the module, i.e., example_ module, is imported here.





                                                           432

                             CITS : IT&ITES - Computer Software Application - Lesson 120 - 137
   440   441   442   443   444   445   446   447   448   449   450