A module is a file containing python definitions( functions and variables) and statements.The file name is the module name with the suffix .py appended.
We need modules to reuse many functions,variables and statements.
To import a module in a program, we use the import keyword along with the name of the module. Ex: import operation. The first time a module is loaded into a running Python script, it is initialized by executing the code in the module once.
Let us write a small piece of code to multiply 2 numbers and also divide them.
This is a program called as operation.py. It contains 2 functions multiply and divide which take in 2 parameters a and b and returns their product and quotient respectively.
Let us write another program.
This program is called myprogram.py . It imports the file called operation.py and prints the product of 2 numbers 4 and 6 using the multiply function of operation.py.
The output is as follows.
Python has a standard list of built in modules. You can check them out here . Python modules
For example, we have math module. To use the math module we use import math statement.