sum = 0 for i in args: sum += i return sum # ————— End of sum.py ————— # You can import by listing the file name minus the py import sum # Get access to functions by proceeding with the file # name and then the function you want print("Sum :", sum.getSum(1, 2, 3, 4, 5)) # ---------- FROM ---------- # You can use from to copy specific functions from a module # You can use from sum import * to import all functions # You can import multiple functions by listing them after # import separated by commas from sum import getSum # You don't have to reference the module name now print("Sum :", getSum(1, 2, 3, 4, 5)) # ---------- EXCEPTION HANDLING ---------- # Exceptions are triggered either when an error occurs # or when you want them to.
import sum print("Sum:", sum.getSum(5, 1, 4))
# I created the a python file named sum # It should be in the same folder as this .py file # A _pycache_ file was created automatically after this code was ran import sum print("Sum:", sum.getSum(1, 2, 3, 4, 5, 34, 123))
# The benefit of using for instead of import is # that you don't have to list the name of the module # whenever you use it. Also you could add more def's by adding their varibles using commas. from sum import getSum print("Sum:", getSum(1,2,3,4,5,34,12,158))