Posts

Showing posts with the label modules

Python Modules

Image
What are modules in Python? Modules refer to a file containing Python statements and definitions. A file containing Python code, for e.g.:  example.py , is called a module and its module name would be  example . We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code. We can define our most used functions in a module and import it, instead of copying their definitions into different programs. Let us create a module. Type the following and save it as  example.py . # Python Module example def add ( a , b ): """This program adds two numbers and return the result""" result = a + b return result Here, we have defined a  function   add()  inside a module named  example . The function takes in two numbers and returns their sum. How to import modules in Python? We can import the definitions inside a module to another module or the interact...