Modules

The main purpose of the module is to split up the code for efficient use. It makes debugging simple, so that the errors can be easily identified. In case, if we write the program in the python Interpreter, the program gets lost as soon as we exit the terminal. But in case of the modules, all the programs are stored in the main memory.

Let create a program to add two numbers. For this first create a python file add.py and type the following program in it.

def sum(int a, int b):

int x = a+b

print(“The sum is %s” %x)

Thus after saving it, if we want to execute this program, first we want to import it.

import add

add.sum(5,7)

Thus when we execute it, we will get the output as 12.

A module can be declared inside another module in which it is said to be sub-module. Thus all the sub-module will be under the module which has a common name. The program which is importing the module can also access its variable too.

Python has provided with some of the module in which it deals with the Operating system. Some of the modules are os, platform, request.

>>> import os

>>> os.getuid()

200

>>> os.getpid()

2772

where getuid returns the id of the currently executing process and in which the getpid returns the parent executing process.

Leave a comment