Python
In this tutorial we will learn about modules in Python.
A module is a file containing Python program and the program can consists of variables, classes and functions.
We use module to organise logically related code in one place so that it can be used easily.
For example we can create a module that contains code to print greeting messages or create a module that computes area.
We can divide modules in two groups.
These are the modules that come with Python. Example of built-in Python module is the Math module.
These are the modules written by developers for custom use.
Any module that you will create or download from online Python repository created by other users are user defined modules.
To create a module all we have to do is save our Python code in a file with .py
extension.
In the following example we are creating a module that contains functions to compute areas. And we are saving this in a file named area.py
File name: area.py
# area of square
def square(x):
return x * x
# area of rectangle
def rectangle(l, b):
return l * b
# area of circle
def circle(r):
return (22 / 7) * (r * r)
To use a module we have to first import it in the file were we want to use it.
We use the import
keyword to import a module.
Syntax:
import moduleName
Where, moduleName
is the name of the module.
When we use the import
keyword we are importing all the content of the module.
In the following example we are importing the complete Math module.
import math
# some code goes here...
We use the following statement to import specific items from a given module.
from moduleName import item1, item2, ...
Where, moduleName
is the name of the module and item1
, item2
are the items that we want to import from the module.
In the following Python program we are only importing the sqrt
method and constant e
from the math module.
from math import sqrt, e
# some code goes here...
We can use from moduleName import *
statement to import all items from the given module.
Importing everything using *
is not a good programming practice. Always import what you need in your program.
We can use the import and from ... import statement to import a built in Python module.
The following Python file greetings.py
is a module consisting of message function.
File name: greetings.py
def message(str = "World"):
return "Hello %s!" %str
In the following Python file helloworld.py
we are importing the greetings
module.
File name: helloworld.py
# import module
import greetings
# some code goes here...
A module can contain variables, constants, functions and classes.
To use the item present inside a module we use the following syntax.
moduleName.item
Where, moduleName
is the name of the module and item
can be a variable, function or class of the module.
The following Python file awesome.py
is a module and it consists of a constant and a function.
# constant
AWESOME_STRING = "Awesome"
# func
def message():
return "This is awesome!"
In the following Python file useAwesome.py
we are using the awesome
module.
import awesome
print(awesome.AWESOME_STRING) # Awesome
print(awesome.message()) # This is awesome!
ADVERTISEMENT