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.
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.
- import <file_name1, file_name2,…file_name(n)=””>
Have a look over an example:
- def add(a,b):
- print c
Save the file by the name addition.py. To import this file “import” statement is used.
- import addition
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)
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:
- def msg_method():
- print “Today the weather is rainy”
- def display_method():
- print “The weather is Sunny”
- import msg,display
- 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.
- from <module_name> import <attribute1,attribute2,attribute3,…attributen>
Have a look over the example:
- def circle(r):
- print 3.14*r*r
- def square(l):
- print l*l
- def rectangle(l,b):
- print l*b
- def triangle(b,h):
- print 0.5*b*h
- from area import square,rectangle
3) To import whole module:
You can import whole of the module using “from? import *”
- from <module_name> import *
Using the above statement all the attributes defined in the module will be imported and hence you can access each attribute.
Same as above example
- from area import *
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.
Let?s have a look over each module:
Using math module , you can use different built in mathematical functions.
|ceil(n)||Returns the next integer number of the given number|
|sqrt(n)||Returns the Square root of the given number.|
|exp(n)||Returns the natural logarithm e raised to the given number|
|floor(n)||Returns the previous integer number of the given number.|
|log(n,baseto)||Returns the natural logarithm of the number.|
|pow(baseto, exp)||Returns baseto raised to the exp power.|
|sin(n)||Returns sine of the given radian.|
|cos(n)||Returns cosine of the given radian.|
|tan(n)||Returns tangent of the given radian.|
Useful Example of math module:
- import math
- print math.ceil(a)
- print math.floor(a)
- print math.sqrt(b)
- print math.exp(3.0)
- print math.log(2.0)
- print math.pow(2.0,3.0)
- print math.sin(0)
- print math.cos(0)
- print math.tan(45)
The math module provides two constants for mathematical Operations:
|Pi||Returns constant ? = 3.14159…|
|ceil(n)||Returns constant e= 2.71828…|
- import math
- print math.pi
- print math.e
The random module is used to generate the random numbers. It provides the following two built in functions:
|random()||It returns a random number between 0.0 and 1.0 where 1.0 is exclusive.|
|randint(x,y)||It returns a random number between x and y where both the numbers are inclusive.|
- import random
- print random.random()
- print random.randint(2,8)
Other modules will be covered in their respective topics.
A Package is simply a collection of similar modules, sub-packages etc..
Steps to create and import Package:
1) Create a directory, say Info
2) Place different modules inside the directory. We are placing 3 modules msg1.py, msg2.py and msg3.py respectively and place corresponding codes in respective modules. Let us place msg1() in msg1.py, msg2() in msg2.py and msg3() in msg3.py.
3) Create a file __init__.py which specifies attributes in each module.
4) Import the package and use the attributes using package.
Have a look over the example:
1) Create the directory:
- import os
2) Place different modules in package: (Save different modules inside the Info package)
- def msg1():
- print “This is msg1”
- def msg2():
- print “This is msg2”
- def msg3():
- print “This is msg3”
3) Create __init__.py file:
- from msg1 import msg1
- from msg2 import msg2
- from msg3 import msg3
4)Import package and use the attributes:
- import Info
- This is msg1
- This is msg2
- This is msg3
What is __init__.py file? __init__.py is simply a file that is used to consider the directories on the disk as the package of the Python. It is basically used to initialize the python packages.