The calculations.py
Python module provides basic arithmetic
operations, including addition, subtraction, multiplication, and division.
Here are a few examples of how to use the functions in calculations.py
:
>>> import calculations
>>> calculations.add(2, 2)
4.0
>>> calculations.subtract(2, 2)
0.0
>>> calculations.multiply(2, 2)
4.0
>>> calculations.divide(2, 2)
1.0
These examples show how to use the calculations.py
module in your code.
# calculations.py
"""Provide several sample math calculations.
This module allows the user to make mathematical calculations.
Module-level tests:
>>> add(2, 4)
6.0
>>> subtract(5, 3)
2.0
>>> multiply(2.0, 4.0)
8.0
>>> divide(4.0, 2)
2.0
"""
def add(a, b):
"""Compute and return the sum of two numbers.
Tests for add():
>>> add(4.0, 2.0)
6.0
>>> add(4, 2)
6.0
"""
return float(a + b)
def subtract(a, b):
"""Calculate the difference of two numbers.
Tests for subtract():
>>> subtract(4.0, 2.0)
2.0
>>> subtract(4, 2)
2.0
"""
return float(a - b)
def multiply(a, b):
"""Compute and return the product of two numbers.
Tests for multiply():
>>> multiply(4.0, 2.0)
8.0
>>> multiply(4, 2)
8.0
"""
return float(a * b)
def divide(a, b):
"""Compute and return the quotient of two numbers.
Tests for divide():
>>> divide(4.0, 2.0)
2.0
>>> divide(4, 2)
2.0
>>> divide(4, 0)
Traceback (most recent call last):
ZeroDivisionError: division by zero
"""
return float(a / b)
python -m doctest -v README.md
Trying:
import calculations
Expecting nothing
ok
Trying:
calculations.add(2, 2)
Expecting:
4.0
ok
Trying:
calculations.subtract(2, 2)
Expecting:
0.0
ok
Trying:
calculations.multiply(2, 2)
Expecting:
4.0
ok
Trying:
calculations.divide(2, 2)
Expecting:
1.0
ok
1 items passed all tests:
5 tests in README.md
5 tests in 1 items.
5 passed and 0 failed.
Test passed.