Modules in Python
Modules in Python
PYTHON
Python Modules
• Modular programming is the practice of segmenting a single, complicated coding task into multiple,
simpler, easier-to-manage sub-tasks.
• Therefore, we can build a bigger program by assembling different modules that act like building blocks.
Simplification
Flexibility
Reusability
Scope
What are Modules in Python?
• 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.
• 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().
• 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.
• 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 )
• 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
• 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
• 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.
• We can import specific names from a module without importing the module as a whole.
Here is an example
• 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 )