Skip to content

Calculations

sandbox.calculations

Provide several sample math calculations.

This module allows the user to make mathematical calculations.

Classes:

  • Two_numbers

    Several sample math calculations using two numbers.

Functions:

  • add

    Compute and return the sum of two numbers.

  • subtract

    Calculate the difference of two numbers.

  • multiply

    Compute and return the product of two numbers.

  • divide

    Compute and return the quotient of two numbers.

Classes

Two_numbers(first, second)

Several sample math calculations using two numbers.

Examples:

Python Console Session
1
2
3
4
>>> from sandbox.calculations import Two_numbers
>>> nums = Two_numbers(4, 2)
>>> print(f'{nums.add()=}')
nums.add()=6.0

Parameters:

Methods:

  • add

    Add my two numbers, and if additional numbers are provided, add them to the sum.

  • subtract

    Subtract my two numbers.

  • multiply

    Multiply my two numbers.

  • divide

    Divide my two numbers.

Attributes:

Source code in sandbox\calculations.py
Python
22
23
24
25
26
27
28
29
30
31
32
def __init__(self, first: Union[float, int], second: Union[float, int]):
    """Initiate my number.

    Args:
        first: the first number 
        second: the second number  
    """
    self.first: Union[float, int]= first
    """The first number."""
    self.second = second
    """The second number."""
Attributes
first: Union[float, int] = first instance-attribute

The first number.

second = second instance-attribute

The second number.

Functions
add(*args)

Add my two numbers, and if additional numbers are provided, add them to the sum.

Returns:

  • Union[float, int]

    A number representing the arithmetic of all the numbers.

Source code in sandbox\calculations.py
Python
35
36
37
38
39
40
41
42
43
def add(self, *args) -> Union[float, int]:
    """Add my two numbers, and if additional numbers are provided, add them to the sum.

    Returns:
        A number representing the arithmetic of all the numbers.
    """
    if len(args) > 0:
        return sum(args) + add(self.first, self.second)
    return add(self.first, self.second)
subtract()

Subtract my two numbers.

Source code in sandbox\calculations.py
Python
45
46
47
def subtract(self) -> Union[float, int]:
    """Subtract my two numbers."""
    return subtract(self.first, self.second)
multiply()

Multiply my two numbers.

Source code in sandbox\calculations.py
Python
49
50
51
def multiply(self) -> Union[float, int]:
    """Multiply my two numbers."""
    return multiply(self.first, self.second)
divide()

Divide my two numbers.

Source code in sandbox\calculations.py
Python
53
54
55
def divide(self) -> Union[float, int]:
    """Divide my two numbers."""
    return divide(self.first, self.second)

Functions

add(a, b)

Compute and return the sum of two numbers.

Examples:

Python Console Session
>>> add(4.0, 2.0)
6.0
>>> add(4, 2)
6.0

Parameters:

  • a (Union[float, int]) –

    A number representing the first addend in the addition.

  • b (Union[float, int]) –

    A number representing the second addend in the addition.

Returns:

  • float

    A number representing the arithmetic sum of a and b.

Source code in sandbox\calculations.py
Python
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def add(a: Union[float, int], b: Union[float, int]) -> float:
    """Compute and return the sum of two numbers.

    Examples:
        >>> add(4.0, 2.0)
        6.0
        >>> add(4, 2)
        6.0

    Args:
        a: A number representing the first addend in the addition.
        b: A number representing the second addend in the addition.

    Returns:
        A number representing the arithmetic sum of `a` and `b`.
    """
    return float(a + b)

subtract(a, b)

Calculate the difference of two numbers.

Examples:

Python Console Session
>>> subtract(4.0, 2.0)
2.0
>>> subtract(4, 2)
2.0

Parameters:

  • a (Union[float, int]) –

    A number representing the minuend in the subtraction.

  • b (Union[float, int]) –

    A number representing the subtrahend in the subtraction.

Returns:

  • float

    A number representing the difference between a and b.

Source code in sandbox\calculations.py
Python
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def subtract(a: Union[float, int], b: Union[float, int]) -> float:
    """Calculate the difference of two numbers.

    Examples:
        >>> subtract(4.0, 2.0)
        2.0
        >>> subtract(4, 2)
        2.0

    Args:
        a: A number representing the minuend in the subtraction.
        b: A number representing the subtrahend in the subtraction.

    Returns:
        A number representing the difference between `a` and `b`.
    """
    return float(a - b)

multiply(a, b)

Compute and return the product of two numbers.

Examples:

Python Console Session
>>> multiply(4.0, 2.0)
8.0
>>> multiply(4, 2)
8.0

Parameters:

  • a (Union[float, int]) –

    A number representing the multiplicand in the multiplication.

  • b (Union[float, int]) –

    A number representing the multiplier in the multiplication.

Returns:

  • float

    A number representing the product of a and b.

Source code in sandbox\calculations.py
Python
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def multiply(a: Union[float, int], b: Union[float, int]) -> float:
    """Compute and return the product of two numbers.

    Examples:
        >>> multiply(4.0, 2.0)
        8.0
        >>> multiply(4, 2)
        8.0

    Args:
        a: A number representing the multiplicand in the multiplication.
        b: A number representing the multiplier in the multiplication.

    Returns:
        A number representing the product of `a` and `b`.
    """
    return float(a * b)

divide(a, b)

Compute and return the quotient of two numbers.

Examples:

Python Console Session
>>> divide(4.0, 2.0)
2.0
>>> divide(4, 2)
2.0
>>> divide(4, 0)
Traceback (most recent call last):
...
ZeroDivisionError: division by zero

Parameters:

  • a (Union[float, int]) –

    A number representing the dividend in the division.

  • b (Union[float, int]) –

    A number representing the divisor in the division.

Returns:

  • float

    A number representing the quotient of a and b.

Raises:

Source code in sandbox\calculations.py
Python
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def divide(a: Union[float, int], b: Union[float, int]) -> float:
    """Compute and return the quotient of two numbers.

    Examples:
        >>> divide(4.0, 2.0)
        2.0
        >>> divide(4, 2)
        2.0
        >>> divide(4, 0)
        Traceback (most recent call last):
        ...
        ZeroDivisionError: division by zero

    Args:
        a: A number representing the dividend in the division.
        b: A number representing the divisor in the division.

    Returns:
        A number representing the quotient of `a` and `b`.

    Raises:
        ZeroDivisionError: An error occurs when the divisor is `0`.
    """
    if b == 0:
        raise ZeroDivisionError("division by zero")
    return float(a / b)