DEV Community

Cover image for Python module in simple way
MHMasuk
MHMasuk

Posted on

Python module in simple way

What is Module in python?

Module is just a file with a .py extension, which contain some Python code. The python code can be a a set of functions or variables.

When do we need to use modules ?

Suppose, we are making an application to generate a diet chart. Based on some information about a person the application will generate a diet chart. So, Every time some set of specific function is calling, to make this simple we can make a module name chart.py with a set of specific functions to reuse in different applications again and again.

Create a Module.

To create a module name chart, we just need to write some code in file and save the file with .py extensions. So, the name of the python file is the name of the module.

To import a module

import chart
Enter fullscreen mode Exit fullscreen mode

Python from and import statement

To import a specific function we can use from and import statement.

from chart import generate
Enter fullscreen mode Exit fullscreen mode

Module renaming

To change the import name of the module we can use as

import chart as ch
Enter fullscreen mode Exit fullscreen mode

Top comments (0)