DEV Community

Ezhil Abinaya K
Ezhil Abinaya K

Posted on

Python Module

Python Modules
Module is a file containing definitions and statements. A module can define functions, classes and variables. Modules help organize code into separate files so that programs become easier to maintain and reuse. Instead of writing everything in one place, related functionality can be grouped into its own module and imported whenever needed.
Create a Module
To create a module, write the desired code and save that in a file with .py extension.
Example: Let's create a function_demo.py in which we define four functions, one add,one sub,one mul and one div.

def add(a,b):
    return a+b
def sub(a,b):
    return a-b
def mul(a,b):
    return a*b
def div(a,b)
    return a//b
developer ="Ezhil"
Enter fullscreen mode Exit fullscreen mode

This is all that is required to create a module.
Import module
Modules can be used in another file using the import statement. When Python sees an import, it loads the module if it exists in the interpreter’s search path. Below is the syntax to import a module:

import module
Enter fullscreen mode Exit fullscreen mode

Example: Here, we are importing the function_demo.py that we created earlier.

import function_demo
result=function_demo.add(100,150)
print(result)
//250
Enter fullscreen mode Exit fullscreen mode

Python is a dynamically typed programming language.
Reference
https://www.geeksforgeeks.org/python/python-modules/

Top comments (0)