Pythonmodules
Pythonmodules
Pythonmodules
A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provide better modularity for your application and a high degree of code reusing.
The concept of module in Python further enhances the modularity. You can define more than one
related functions together and load required functions. A module is a file containing definition of
functions, classes, variables, constants or any other Python object. Contents of this file can be
made available to any other program. Python has the import keyword for this purpose.
A module in Python is a single file that contains a collection of related functions, classes, and
variables. It allows you to organize your code into reusable, modular units and also enables code
reuse across different projects. Modules are typically used to group related functionality together,
such as mathematical operations, file handling, or data processing. They can be imported into
other Python scripts using the built-in import statement, which allows you to use the functions
and classes defined in the module in your script.
Modules
A module is simply a Python file with a .py extension that can be imported inside another Python
program.
Advantages of modules –
Built in Modules
Python's standard library comes bundled with a large number of modules. They are called built-
in modules. Most of these built-in modules are written in C (as the reference implementation of
Python is in C), and pre-compiled into the library. These modules pack useful functionality like
system-specific OS management, disk IO, networking, etc.
os
Any text file with .py extension and containing Python code is basically a module. It can contain
definitions of one or more functions, variables, constants as well as classes. Any Python object
from a module can be made available to interpreter session or another Python script by import
statement. A module can also include runnable code.
Create a Module
Creating a module is nothing but saving a Python code with the help of any editor. Let us save
the following code as mymodule.py
def SayHello(name):
print ("Hi {}! How are you?".format(name))
return
import mymodule
mymodule.SayHello("Harish")
In Python, the import keyword has been provided to load a Python object from one module. The
object may be a function, class, a variable etc. If a module contains multiple definitions, all of
them will be loaded in the namespace.
def sum(x,y):
return x+y
def average(x,y):
return (x+y)/2
def power(x,y):
return x**y
The import mymodule statement loads all the functions in this module in the current
namespace. Each function in the imported module is an attribute of this module object.
>>> dir(mymodule)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__', 'average', 'power', 'sum']
To call any function, use the module object's reference. For example, mymodule.sum().
import mymodule
print ("sum:",mymodule.sum(10,20))
print ("average:",mymodule.average(10,20))
print ("power:",mymodule.power(10, 2))
sum:30
average:15.0
power:100
The from ... import Statement
The import statement will load all the resources of the module in the current namespace. It is
possible to import specific objects from a module by using this syntax. For example −
Out of three functions in mymodule, only two are imported in following executable script
example.py
sum: 30
average: 15.0
Note that function need not be called by prefixing name of its module to it.
It is also possible to import all the names from a module into the current namespace by using the
following import statement −
This provides an easy way to import all the items from a module into the current namespace;
however, this statement should be used sparingly.