Modules allude to a File containing Python statements and definitions. A File containing Python code, for Example example.py, is known as a module and its module name would be model.
We use modules to separate vast projects into little reasonable and sorted out Files. Besides, modules give reusability of code.
We can characterize our most utilized capacities in a module and import it, rather than replicating their definitions into various projects.
Python Module precedent
This program includes two numbers and returns the outcome
def add(a, b): result = a + b return result
How to import modules in Python?
We can import the definitions inside a module to another module or the intuitive mediator in Python. We utilize the import catchphrase to do this. To import our recently characterized module precedent we type the accompanying in the Python brief.
This does not enter the names of the capacities characterized in model straightforwardly in the present image table. It just enters the module name precedent there.
Utilizing the module name we can get to the capacity utilizing dab (.) task. For instance:
Python import statement
We can import a module utilizing import statement and access the definitions inside it utilizing the spot administrator as depicted previously. Here is a precedent.
import statement precedent
To import standard module math
import math print("The estimation of pi is", math.pi)
Import with renaming
We can import a module by renaming it as pursues.
import module by renaming I
import math as m print("The estimation of pi is", m.pi)
Python from…import statement
We can import explicit names from a module without bringing in the module all in all. Here is a precedent.
import just pi from math module
from math import pi print("The estimation of pi will be", pi)
We imported just the property pi from the module.
In such case we don’t utilize the speck administrator. We could have imported different qualities as pursues.
from math import pi, e >>pi 3.141592653589793 >>e 2.718281828459045
Import all names
We can import all names(definitions) from a module utilizing the accompanying develop.
import all names from the standard module math
from math import * print("The estimation of pi will be", pi)