Write Code Make Seperate Py File Run New Python Module Q37027909

WRITE A CODE TO MAKE A SEPERATE .py File run in a newpython module.


Solution


1.#addition.py -> this is a separate .py filecontains one function add() only.

def add(a, b):
   print(a, b)
   return a+b

———————————————————————————————————————————————————————-

2.#main.py -> this is a new .py module in which wecall or run addition.py file.

import addition # Imported addtion.py file

if __name__ == ‘__main__’:
  
   a = 10
   b = 20
   print(addition.add(a, b)) #addition.add()means called function which is in addition.py file.

# If you, only want to call function add from you addition.pyfile then instead of doing “import addition” youcan write “from addition import add” and use it as”add(a, b)

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.