Types of Modules

2 min read ·

Python modules are mainly divided into three types based on their source and usage.

Built-in Modules

These modules are already available in Python. You do not need to install them separately.
They come pre installed with Python and provide commonly used functionalities.
Some examples:
  • math
  • sys
  • random
  • datetime

Example using math module


Example using random module


Example using datetime module


Note

Built in modules save time because you do not need to write common functionalities from scratch.


User defined Modules

These are modules created by developers to organize their own code.
You create a Python file and reuse it in other programs.

Step 1: Create a module

File name: my_module.py

Step 2: Use the module


Pro Tip

User defined modules help in breaking large programs into smaller manageable parts.


Third party Modules

These modules are created by other developers and are not included with Python by default.
You need to install them using pip.

Installing a module


Example using third party module


Caution

Make sure pip is installed and internet connection is available before installing third party modules.


Key Difference

  • Built-in modules → Already available in Python
  • User-defined modules → Created by you
  • Third-party modules → Installed using pip

Exercise

Create your own module named operations.py:
  • Add a function to multiply two numbers
  • Add a function to divide two numbers
Then import it and use both functions in another file.