Modules

 

  • In python when our program gets longer, we may want to split it into several files for easier maintenance.

  • we may also want to use a handy function that we written in several programs without copying its definition into each program.

  • To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter.

  • Such a file is called a module; definitions from a module can be imported into other modules or into the main module.

Create a file called module1.py with following code:

'''module demo'''

def sum(a,b):

ans=a+b

print("Sum is ", ans)

Now create another file called call_module.py file with following code:

'''calling a module'''

import module1

module1.sum(3,4)

Output:

('Sum is ', 7)