What are modules? Explain types of statements provided to load modules?
Ans:-A python module can be defined as a python program file
which contains a python code including python functions, class or variables. In
other words we can say that our python code file saved with the
extension(.py)is treated as a module. We may have a run able code inside the
python module.
Modules in python provides us the flexibility to organize the
code in a logical way.
To use the functionality of one module into another, we must
have to import the specific module.
EXAMPLE:-
In this example, we will create a module named as file.py
Def displayMsg(name)
Print(“Hi”+name);
STATEMENTS USED TO LOAD THE PYTHON MODULE:-
We need to load the module in our python code to use its
functionality.Python provides two types of statements:-
(i)The import statement
(ii)The from-import statement
1.The import statement:-The import statement is used to
import all the functionality of one module into another.
We can import multiple modules with a single import
statement.
SYNTAX:-
Import module1,module2,……..modulen
EXAMPLE:-
Import file;
name=input(“Enter the name”)
file.displayMsg(name)
2.The from-import statement:-Instead of importing the whole
module into the namespace,python provides the flexibility to import only the
specific attributes of a module.
SYNTAX:-
From<module-name>import<name1>,<name2>,<name3>
calculation.py
def summation(a,b):
return
a+b;
def multiplication(a,b)
return
a*b;
def divide(a,b)
return
a/b;
Main.py
from calculation import summation
a=int(input(“enter the first number”))
b=int(input(“enter the second number”))
print(“sum=”,summation(a,b))