Page 280 - Computer Science Class 11 With Functions
P. 280

11                                                                      MODULES














          Chapter Outline


          11.1 Built-in Modules                              11.2 User-defined Functions Revisited
          11.3 User-defined Modules







        Introduction

        In  the  last  chapter  on  Python  Functions,  we  discussed  several  predefined  built-in  functions  such  as  input(),
        print(), int(), and abs(), dealing with input-output, data type conversion, and mathematical operations.
        We also learnt to develop user-defined functions to perform some simple, useful tasks. However, Python also provides
        specialised modules for specific tasks such as math  and statistics,  which provides several functions for
        mathematical and statistical computations. Similarly, the random  module provides several functions for random
        number generation. We will also develop some user-defined functions. Sometimes, a user may like to specify values
        of only the first few parameters because other parameters may take preassigned values. The preassigned values are
        called default values, and the corresponding parameters are called default parameters. Also, there may be situations
        where we need to specify only a few arguments, possibly in a sequence different from the list of formal parameters,
        we will study the use of keyword arguments.

        11.1 Built-in Modules

        A Python module is just a Python script. Python provides several modules, each of which contains several useful
        functions. Often, it is convenient to use predefined functions available in Python modules. However, to use these
        functions, we need to import the relevant module using the following syntax:

        import <modulename>
        Once a module is imported, we can use all the functions defined in the module by preceding function name by the
        name of the module and a dot(.) operator as shown below:

        <modulename>.<functionname>()
        For example, to use the sqrt() function defined in the math module, we first import the math module and then use
        sqrt() function to find the square root of 25, as follows:
         >>> import math
         >>> math.sqrt(25)
              5
         278   Touchpad Computer Science-XI
   275   276   277   278   279   280   281   282   283   284   285