0% found this document useful (0 votes)
5 views

Modules in Python

Uploaded by

Bahubali C
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Modules in Python

Uploaded by

Bahubali C
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

PYTHON MODULES

• A module is a collection of variables and functions that are


grouped together in a single file.
• The variables and functions in a module are usually related to
one another in some way.
• Ex: Module math contain the variable pi and mathematical
functions such as cos(cosine) and sqrt.
• Modular programming is the practice of segmenting a single,
complicated coding task into multiple, simpler, easier-to-manage
sub-tasks. We call these subtasks modules. Therefore, we can
build a bigger program by assembling different modules that act
like building blocks.
• Benefits of modular programming:
• Simplification:
• Flexibility:
• Reusability:
• Scope:
Importing Modules:
• To gain access to the variables and functions from a module, we need to
import it.
import math
Above statement imports math module to use the functions present in math module.

Importing a module creates a new variable with that name. And that name refers to an
object whose type id module:
type(math)
<class ‘module’>

The built in function help can be used to see what the module contains.
help(math)
• In Python, we may import functions from one module into our program.
• For this, we make use of the import keyword. In the Python window, we
add the next to import keyword, the name of the module we need to
import.
• Example : import example_module
• The functions that we defined in the example_module are not
immediately imported into the present program. Only the name of the
module, i.e., example_ module, is imported here.

• We may use the dot operator to use the functions using the
module name. For instance:
• There are several standard modules for Python. The complete list
of Python standard modules is available. The list can be seen
using the help command.
• Similar to how we imported our module, a user-defined module,
we can use an import statement to import other standard
modules.
• Importing a module can be done in a variety of ways. Below is a
list of them.
Python import Statement
• Using the import Python keyword and the dot operator, we may
import a standard module and can access the defined functions
within it. Here's an illustration.
Importing and also Renaming
• While importing a module, we can change its name too. Here is an example to show.

• The math module is now named mt in this program. In some circumstances, it


might help us type faster in case of modules having long names.
• Please take note that now the scope of our program does not include the term
math. Thus, mt.pi is the proper implementation of the module, whereas math.pi
is invalid.
Python from...import Statement
• We can import specific names from a module without importing
the module as a whole. Here is an example.
Import all Names - From import * Statement
• To import all the objects from a module within the present
namespace, use the * symbol and the from and import keyword.
Syntax:
from name_of_module import *
• There are benefits and drawbacks to using the symbol *. It is not
advised to use * unless we are certain of our particular
requirements from the module; otherwise, do so.
• Here is an example of the same.
Locating Path of Modules
• The interpreter searches numerous places when importing a module in
the Python program. Several directories are searched if the built-in
module is not present. The list of directories can be accessed using
sys.path. The Python interpreter looks for the module in the way
described below:

• The module is initially looked for in the current working directory. Python
then explores every directory in the shell parameter PYTHONPATH if the
module cannot be located in the current directory. A list of folders
makes up the environment variable known as PYTHONPATH. Python
examines the installation-dependent set of folders set up when Python
is downloaded if that also fails.
The dir() Built-in Function
• We may use the dir() method to identify names declared within a
module.
• For instance, we have the following names in the standard
module str. To print the names, we will use the dir() method in
the following way:
Code
# Python program to print the directory of a module
print( "List of functions:\n ", dir( str ), end=", " )
Output: Displays list of functions wrt str
Create a Module:
A module is a file containing Python code, definitions of functions,
statements, or classes.
An example_module.py file is a module we will create and whose name is
example_module.
We employ modules to divide complicated programs into smaller, more understandable
pieces.
Modules also allow for the reuse of code.

To create a module just save the code you want in a file with the file extension .py:
• To import the created module, we make use of import keyword
followed by name of the module which we want to import.
import example_module

The functions that we defined in the example_module are not immediately


imported into the present program. Only the name of the module, i.e.,
example_ module, is imported here.
We may use the dot operator to use the functions using the module name.
For example:

You might also like