Module

Modules are used to categorize code in Python into smaller part. A module is simply a file, where classes, functions and variables are defined. Grouping similar code into a single file makes it easy to access.

Have a look over example:

If the content of a book is not indexed or categorized into individual chapters, then the book might have turned boring and hectic. Hence, dividing book into chapters made it easy to understand.

In the same sense python modules are the files which have similar code. Thus module is simplify a python code where classes, variables and functions are defined.

Advantage:

Python provides the following advantages for using module:

1) Reusability: Module can be used in some other python code. Hence it provides the facility of code reusability.

2) Categorization: Similar type of attributes can be placed in one module.

Importing a Module:

There are different ways by which you we can import a module. These are as follows:

1) Using import statement:

"import" statement can be used to import a module.

Syntax:

import <file_name1, file_name2,...file_name(n)="">
</file_name1,>
Have a look over an example:

eg:

def add(a,b):
    c=a+b
    print c
    return

Save the file by the name addition.py. To import this file "import" statement is used.

import addition
addition.add(10,20)
addition.add(30,40)

Create another python file in which you want to import the former python file. For that, import statement is used as given in the above example. The corresponding method can be used by file_name.method (). (Here, addition. add (), where addition is the python file and add () is the method defined in the file addition.py)

Output:

>>>  
30
70
>>>

NOTE: You can access any function which is inside a module by module name and function name separated by dot. It is also known as period. Whole notation is known as dot notation.

Example of importing multiple modules:

Eg:

1) msg.py:

def msg_method():
    print "Today the weather is rainy"
    return

2) display.py:

def display_method():
    print "The weather is Sunny"
    return

3) multiimport.py:

import msg,display
msg.msg_method()
display.display_method()

Output:

>>>  
Today the weather is rainy
The weather is Sunny
>>>      

2) Using from.. import statement:

from..import statement is used to import particular attribute from a module. In case you do not want whole of the module to be imported then you can use from ?import statement.

Syntax:

from  <module_name> import <attribute1,attribute2,attribute3,...attributen>    
</attribute1,attribute2,attribute3,...attributen></module_name>

Have a look over the example:

1) area.py

Eg:

def circle(r):
    print 3.14*r*r
    return
 
def square(l):
    print l*l
    return
 
def rectangle(l,b):
    print l*b
    return
 
def triangle(b,h):
    print 0.5*b*h
    return

2) area1.py

from area import square,rectangle
square(10)
rectangle(2,5)
Output:

>>>  
100
10
>>>

3) To import whole module:

You can import whole of the module using "from? import *"

Syntax:

from <module_name> import *
</module_name>
Using the above statement all the attributes defined in the module will be imported and hence you can access each attribute.

1) area.py

Same as above example

2) area1.py

from area import *
square(10)
rectangle(2,5)
circle(5)
triangle(10,20)

Output:

>>>  
100
10
78.5
100.0
>>>

Built in Modules in Python:

There are many built in modules in Python. Some of them are as follows:

math, random , threading , collections , os , mailbox , string , time , tkinter etc..

Each module has a number of built in functions which can be used to perform various functions.