Python module is a file with a
.py extension , containing a set of functions that you want to include in your applications. The name of the module will be the name of the
Python file . It allows you to logically
organize your Python code . Grouping related code into a module makes the code easier to understand and use. definitions from a module can be imported into other modules or into the
main module .
Modules can be either:
- User-defined
- Built in
User-defined Modules
How to create a new Python Module?
To create a new module, create a Python file with a
.py extension .
def sum(x,y):
print("Sum of numbers : ", x+y)
Save above code in a file named
"addition.py" .
How to use a Python Module?
Next step is to call the module you just created ("addition.py") in another Python source file, by
using the import statement .
import addition
addition.sum(15,12)
When the above code is executed, the following output is produced:
Create an alias for Python module
You can create an alias for your Python module when you
import a module , by using the as keyword:
Built-in Modules
Python comes with several
built-in modules , which you can import in your Python files whenever you like.
For example, Python has a built-in module
"math" .