Modules
A Module is a Simple Python File. The Module contains Statements, Functions, Definitions.
A Python Module can be used in our Python Program using the import keyword.
Now let's see how we can create a Module in Python.
Creating a Module:
Now I have created a Python Module named details.Py. If we want to use this Module before Name, Location, Website and Function in our Python Program, we must first give the Module inside our Program. There are two ways to give the Module inside the Program.
Import Statement
From Import Statement
Let us have a detailed look at the two statements given above.
Import Statement:
Syntax:
Import Module 1, Module 2,...,Module N
A Module can be used inside our Program by using Import Keyword. More than one Module can be used in the Program by using Import Statement.
Programme:
details.py
def name():
print("Vijanthi Tutorial")
def website():
print("www.vijanthitutorial.com")
def location():
print("Matha Nagar")
program:
Bio data.py
import details
details.name()
A Module called Details has been imported into this Python Program called Bio data.
import details
import is the keyword.
details is the module name.
details.name() --> This statement calls the function named name in the module named details. Now the output vijayanthi tutorial of running the bio data program is available.
The reason is that the print statement vijayanthi tutorial is given inside the name() function.
From Import Statement
There are many functions in a module. If you want to use only one specific function from that module, then this From Import Statement is used.
Programme:
Biodata.py
from details import location
print(location())
Description:
There are a lot of functions in the details module. In this only the location function is required. The from import statement is used only at such times.
from details import location In this statement we use only the location function in the details module. Now when we run the program, the output Matha Nagar is available.
Let's see how to rename a Module.
If you want to change the name of a module, you can change the name of the module using As keyboard.
Syntax:
Import Old-Module-Name as New-Module-Name
Programme:
Biodata.py
import details as info
info.website()
Description:
import details as info --> This statement changes the module name given as details to the module name info.
info.website() --> info calls the module's website() function.
dir() Function:
This is a predefined function. This dir() function displays all the functions, variables and sub modules in a module in a list format.
Biodata.py
import details
print(dir(details))
output:
-------
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'details', 'location', 'name', 'website']
Explain:
print(dir(details)) --> In this statement, the module named details is given inside the function named dir(). Here the dir() function displays all the functions inside the details module in the form of a list.
You want to get certificate click. This button.