Programs · Program 7-16
Q.Program 7-16: Create a user defined module basic_math that contains the following user defined functions:
- To add two numbers and return their sum.
- To subtract two numbers and return their difference.
- To multiply two numbers and return their product.
- To divide two numbers and return their quotient and print “Division by Zero” error if the denominator is zero.
- Also add a docstring to describe the module. After creating module, import and execute functions.
Odisha ChseTextbookSubjective· 5mImportance★★★★★est
45% · 18/40 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Program 7-16 creates a user-defined module: a file basic_math.py holding four documented arithmetic functions (divide guards against zero), which a second program imports and executes.
The idea — module creation and import. Any .py file is a module. Placing related functions in one file gives them a shared, importable home; the triple-quoted string at the top becomes the module's docstring, retrievable as basic_math.__doc__ or via help(basic_math).
Step 1 — create the module (save as basic_math.py):
# basic_math.py
"""basic_math - simple arithmetic on two numbers: add, subtract, multiply, divide."""
def add(x, y):
"""Return the sum of x and y."""
return x + y
def subtract(x, y):
"""Return the difference x - y."""
return x - y
def multiply(x, y):
"""Return the product of x and y."""
return x * y
def divide(x, y):
"""Return x / y, or print an error if y is zero."""
if y == 0:
print("Division by Zero")
else:
return x / y
Step 2 — import and execute the functions (a separate program in the same folder):
# use_basic_math.py
import basic_math
print(basic_math.__doc__)
print("Sum :", basic_math.add(12, 4))
print("Difference :", basic_math.subtract(12, 4))
print("Product :", basic_math.multiply(12, 4))
print("Quotient :", basic_math.divide(12, 4))
basic_math.divide(12, 0) # triggers the error message
Expected output
basic_math - simple arithmetic on two numbers: add, subtract, multiply, divide.
Sum : 16
Difference : 8
Product : 48
Quotient : 3.0
Division by Zero
Key points
import basic_mathruns the module file once and makes its names available asbasic_math.add, etc. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.