Python Data Persistence – Function

Python Data Persistence – Function

The function is called a structured approach to program development. Idea is to break the algorithm into one or smaller, independent, and reusable blocks of instructions. Each such block, designed to perform a single task is known as a function. It is called wherever it is required in the main routine of the program. When called, each function performs the process as defined in it and returns the control back to the main routine, (figure 3.2)

IMG

Thus the function is an important building block of any software. Python interpreter itself has quite a few in-built functions. You have already come across some of them. In addition, standard Python installation a library of many built-in modules (nearly 500 modules are bundled in standard Python distribution), each one having some functions defined in it. While built-in functions are always available, functions in other built-in modules have to be explicitly loaded. Remember we used a randint () function to generate a random integer as a secret number in the previous chapter (secret-number.py). Let us look at another function that calculates the square root of a number. The sqrt () function is defined in the math module. (The math module itself contains nearly 50 functions.) You need to import this module for that purpose.

Example

>>> import math
>>> math.sqrt(100)
10.0
>>>

As you can see, the Python interpreter has access to a large number of predefined functions, either built-in or built-in modules. You can employ these functions in your program as per requirement. You can of course develop your own function if you don’t find a suitable function in any of the built-in modules. These built-in modules are appropriately named to indicate the type of functions in them. Examples are math module (having mathematical functions) and random module (having random number generation functions).

Built-in modules are generally written in C and bundled with Python interpreter in precompiled form. A built-in module may be a Python script (with .py extension) containing useful utilities. A module may contain one or more functions, classes, variables, constants, or any other Python resources.

To display a list of all available modules, use the following command in the Python console:

>>> help(‘modules’)

Python has an import keyword to load all contents of a module in the current namespace of the Python interpreter. Functions can then be called using dot operator as follows:

Example

import module
module.function ( [ arguments if any ] )

Python also provides from keyword that allows only specified functions from a module rather than all functions. The sqrt () function from the math module can also be imported as follows:

Example

>>> from math import sqrt
>>> sqrt(100)
10.0
>>>

We shall first get ourselves acquainted with some of the frequently required functions in built-in modules before we learn how to define and use our own functions.