KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

How do modules help in code reusability? Explain with the help of an example.

Python Modules

2 Likes

Answer

Modules help in code reusability by allowing a program or part of a program to be used again without rewriting it.

One of the advantages of using modules is:

  • Reusability: It minimises the development of redundant codes.
  • Simplicity: It is simple to reuse a code than to write a program again from the beginning.

When related code such as functions and variables is stored in a module, it can be imported and reused in different programs, which saves time and effort.

Example:

The math module contains predefined mathematical functions and variables. It can be reused in a program as shown below:

import math
print("The value of pi is", math.pi)

Here, the value of pi is reused from the math module instead of writing the code again, which shows how modules help in code reusability.

Answered By

2 Likes


Related Questions