Introduction:
The “basic_math” module is designed to perform basic arithmetic operations on numbers. It contains user-defined functions for addition, subtraction, multiplication, and division. This module is useful for performing simple calculations in various applications.
Code:
#The requirement is:
#1. Write a docstring describing the module.
#2. Write user defined functions as per the specification.
#3. Save the file.
#4. Import at shell prompt and execute the functions.
"""
basic_math Module
*******************
This module contains basic arithmetic operations
that can be carried out on numbers
"""
#Beginning of module
def addnum(x,y):
return(x + y)
def subnum(x,y):
return(x - y)
def multnum(x,y):
return(x * y)
def divnum(x,y):
if y == 0:
print ("Division by Zero Error")
else:
return (x/y)
#End of module
Logic:
- The module defines four user-defined functions:
addnum(x, y): This function takes two numbers as input and returns their sum.
subnum(x, y): This function takes two numbers as input and returns their difference.
multnum(x, y): This function takes two numbers as input and returns their product.
divnum(x, y): This function takes two numbers as input and returns their quotient, except when the divisor is zero, in which case it prints a “Division by Zero Error” message. - The “addnum” function adds the two input numbers and returns their sum.
- The “subnum” function subtracts the second input number from the first input number and returns the difference.
- The “multnum” function multiplies the two input numbers and returns their product.
- The “divnum” function checks if the second input number is zero. If it is zero, it prints a “Division by Zero Error” message. Otherwise, it returns the quotient of the first input number divided by the second input number.
Output:
- The output of calling any of the functions in the “basic_math” module will depend on the input provided.
- For example, calling the “addnum” function with inputs of 5 and 3 will return 8.
- Calling the “divnum” function with inputs of 10 and 2 will return 5.
- In other case, Calling the “divnum” function with inputs of 10 and 0 will print “Division by Zero Error”.