Monday, April 9, 2012

Importing Modules

Hello everyone, in this post I will discuss about Modules and how to import Modules.

What are Modules?

Modules are functions created by people that displays a certain output when imported.

Ok so far we know how to make a Class and Functions and how to use them, that's good, but the problem is once the Python window is closed, we cannot use the Functions created in a program for a different Program.

So here comes Modules, we make Modules and save it in places where Python looks for when we import the Module, Pythons root directory is one of the location where it searches for modules. So with this technique, we can use a Module with as many different programs we want.

Lets check this small example.

Open the IDLE, and the IDLE text editor by pressing Ctrl+n

#Sample Module
def test():
    print "I am a Sample Module"

And now save it as directly in the Python27 Directory(C:\Python27) and close the editor. So what we saved is a Module, and the name of the Module is sample, and this module has a test function that prints "I am a Sample Module"

Now on the IDLE

>>> import sample <--- Module Imported
>>> sample1.test()
I am a Sample Module

Now even if we restart the Python Shell or make a new Program, and if we Import the sample module, and call the test function, we would get the same output.

Hope this makes the Basics clear about the Modules. Now lets make a small Program That would import add, subtract modules in any program.

For this we need to make two Modules, Add and Subtract, and to be very basic lets say these modules works with two numbers. So here we Go.


#The Add Module which finds the addition of two numbers
def add(x,y):
    z = x + y
    return z

Save it as, so the module name is add


#Module for Subtraction
def sub(x,y):
    z = x - y
    return z

Save it as, so the module name is sub

Now that we have created two modules we can use them with any programs, below is an example program


#This is a Program where we import modules and work
import add
import sub
#Note if we are importing more than one module, we usually write them in one statement, separating them with a comma, so we can rewrite this as import add, sub
choice = input("Enter 1 for add, 2 for subtract: ")
x = input("Enter the First Number: ")
y = input("Enter the Second Number: ")
if choice==1:
    r = add.add(x,y)
    print "The Addition of ", x, " and ",y, " is ",r
    r = sub.sub(x,y)
    print "The Subtraction of ", x, " and ",y, " is ",r
raw_input("Press Enter To Exit")


Enter 1 for add, 2 for subtract: 1
Enter the First Number: 23
Enter the Second Number: 43
The Addition of  23  and  43  is  66
Press Enter To Exit
>>>=========== RESTART ==============
Enter 1 for add, 2 for subtract: 2
Enter the First Number: 40
Enter the Second Number: 20
The Subtraction of  40  and  20  is  20
Press Enter To Exit

Okay so I Hope this should make the things clear about what a Module is, and how to work with it.

Practice it, as this is one of the main concept used in Python scriptings.

Thank You and Don't Forget to check the next post.


Post a Comment