MODULES IN
PYTHON
Python Modules
• What is Modular Programming?
• 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.
Modularizing our code in a big application has a lot of benefits.
Simplification
Flexibility
Reusability
Scope
What are Modules in Python?
• A document with definitions of functions and various statements written in Python is
called a Python module.
• In Python, we can define a module in one of 3 ways:
• Python itself allows for the creation of modules.
• Similar to the re (regular expression) module, a module can be primarily written in C
programming language and then dynamically inserted at run-time.
• A built-in module, such as the itertools module, is inherently included in the interpreter.
• 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.
• Rather than duplicating their definitions into several applications, we may define our
most frequently used functions in a separate module and then import the complete
module.
Let's construct a module. Save the file as example_module.py after entering the following.
1.# Here, we are creating a simple Python program to show how to create a module.
2.# defining a function in the module to reuse it
3.def square( number ):
4. # here, the above function will square the number passed as the input
5. result = number ** 2
6. return result # here, we are returning the result of the function
• Here, a module called example_module contains the definition of the function square().
• The function returns the square of a given number.
How to Import Modules in Python?
• In Python, we may import functions from one module into our program, or as we say into,
another module.
• For this, we make use of the import Python keyword.
• In the Python window, we add the next to import keyword, the name of the module we
need to import.
• We will import the module we defined earlier example_module.
Syntax: 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:
Example:
1.# here, we are calling the module square method and passing the value 4
2.result = example_module.square( 4 )
3.print("By using the module square of number is: ", result )
• 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.
Code
1.# Here, we are creating a simple Python program to show how to import a standard module
2.# Here, we are import the math module which is a standard module
3.import math
4.print( "The value of euler's number is", math.e )
5.# here, we are printing the euler's number from the math module
The value of euler's number is 2.718281828459045
Importing and also Renaming:
• While importing a module, we can change its name too. Here is an example to show.
Code
1.# Here, we are creating a simple Python program to show how to import a module and re
name it
2.# Here, we are import the math module and give a different name to it
3.import math as mt # here, we are importing the math module as mt
4.print( "The value of euler's number is", mt.e )
5.# here, we are printing the euler's number from the math module
The value of euler's number is 2.718281828459045
• The math module is now named mt in this program. In some situations, 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
1.from math import e
2.# here, the e value represents the euler's number
3.print( "The value of euler's number is", e )
The value of euler's number is 2.718281828459045
• Only the e constant from the math module was imported in this case.
• We avoid using the dot (.) operator in these scenarios. As follows, we may import many
attributes at the same time:
• Code
• # Here, we are creating a simple Python program to show how to import multiple
• # objects from a module
• from math import e, tau
• print( "The value of tau constant is: ", tau )
• print( "The value of the euler's number is: ", e )
The value of tau constant is: 6.283185307179586
The value of the euler's number is: 2.718281828459045