Showing posts with label Importing Modules. Show all posts
Showing posts with label Importing Modules. Show all posts

Tuesday, April 10, 2012

More On Modules

Hello Everyone, in this post, I Will Discuss something more about Modules, its very interesting, so lets get started.

So far we know what a Module is and how to use them. But as We know that Modules are created by Python Users, for some specific outputs or results. Well If I make a Module and use it in my programs, I am aware of the functions in that Module. But if I use a Module created by someone else or rather the Python Builtin Modules, how am I supposed to know what are the functions of the Modules. And what does the Module do.

Well lets create a Module and try to understand with the example. The Same applies for any other Module that you will come across in Your Python Life.

Lets create a Module:

#SimpleMath Module
def __doc__():
    print "This is a simple Math Module Which Can Add Or Subtract"
def add(x,y):
    z = x + y
    return z
def sub(x,y):
    z = x - y
    return z

Save it as simplemath.py under C:/python27

Now On the Python Shell, what can you to do understand the Module,

>>> import simplemath
>>> #First we need to import the Module and then proceed
>>> dir(simplemath)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'add', 'sub']
>>> #dir(modulename) --> Lists the functions and variable name used in the Module
>>> help(simplemath)
Help on module simplemath:

NAME
    simplemath - #SimpleMath Module

FILE
    c:\python27\simplemath.py

FUNCTIONS
    add(x, y)
   
    sub(x, y)


>>> #help(modulename) - returns a detailed report of the Module
>>> help(simplemath.add)
Help on function add in module simplemath:

add(x, y)

>>> #help(modulename.functionname) -->displays the specific report of the function
>>> simplemath.__doc__()
This is a simple Math Module Which Can Add Or Subtract
>>> #modulename.__doc__() --> found on almost all standard Module, which returns a short description about what the Module does.

Well This was just a simple Module that I created for explanation, there exists a Huge Library Of Modules. And to understand any Module these steps helps every-time.

Few Builtin Modules that you would Like To Practice with are: math, urllib, urllib2, system, etc

Check them out, and try to understand them as Modules are used heavily in Python Scripts.

Thank You and Don't Forget to check My Next Post.

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 sample.py 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.

1.

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

Save it as add.py, so the module name is add

2.

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

Save it as sub.py, 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

3.

#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
else:
    r = sub.sub(x,y)
    print "The Subtraction of ", x, " and ",y, " is ",r
raw_input("Press Enter To Exit")

Outputs:

>>>
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.