How to Create Modules in Python 3

In this lesson we want to learn How to Create Modules in Python 3.

 

What are Python Modules ?

Module in Python is file that contains definitions, functions, classes and statements that can be reused in multiple programs. it provides a way to structure and organize your code and making it easier to maintain and reuse the code.

Each module has unique name and can be imported into another Python program using the “import” statement. the imported module acts as namespace, allowing you to access its functions, classes and variables using dot notation (e.g. module_name.function_name).

for example, you could have module named “math_functions.py” that contains various mathematical functions and then import that module into another script to use its functions. this allows you to write reusable, modular code that can be easily shared and maintained.

In addition to user defined modules, Python also includes number of built in modules that provide common functionality, such as the “math” module for mathematical operations, the “os” module for interacting with the operating system, and the “json” module for working with JSON data.

 

 

Learn More on Python

 

 

 

How to Create Modules in Python 3 ?

In Python module is a file that contains definitions, functions and statements. you can use modules to organize your code and reuse it across multiple projects.

These are the steps to create module in Python 3:

  1. Choose a file name for your module and save it with a .py extension. for example, you can name your module “my_module.py”.
  2. Define functions, classes and variables inside the file. for example:

 

 

  1. To use the module in another script, you can use the “import” statement followed by the name of the module:

 

 

Note: You can also import specific objects from the module using the “from” statement:

This way, you can use the imported objects directly in your script without having to qualify them with the name of the module.

Leave a Comment