DEV Community

Kiruthiga S
Kiruthiga S

Posted on

Python

Modularity
Modularity refers to the concept of making multiple modules first and then linking and combining them to form a complete system
Modularity enables re-usability and will minimize duplication

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.

To create a module

def add(x, y):
    return (x+y)
print(add(2,3))
def sub(x, y):
    return (x-y)
print(sub(12,2))
Enter fullscreen mode Exit fullscreen mode

Output:5
10

Types of Modules

  • Built-in Modules
  • User-Defined Modules
  • External (Third-Party) Modules
  • Package Modules

Import the module
Modules can be used in another file using the import statement

import module
Enter fullscreen mode Exit fullscreen mode
import calc
print(calc.add(10, 2))
Enter fullscreen mode Exit fullscreen mode

Output:12

ModuleNotFoundError
ModuleNotFoundError in Python means the interpreter cannot find the library or file you are trying to import.

Top comments (0)