Skip to content

Arithmetic operations

Class that performs basic arithmetic operations.

Source code in src/calculator.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Calculator:
    """Class that performs basic arithmetic operations."""

    def add(self, x: float, y: float):
        """A method for addition that takes two arguments and returns their sum.

        Parameters
        ----------
        x: float
            First summand.
        y: float
            Second summand.

        Returns
        -------
        float
            Sum of the arguments.
        """
        return x + y

    def subtract(self, x: float, y: float):
        """A method for subtraction that takes two arguments and returns their difference.

        Parameters
        ----------
        x: float
            Minuend.
        y: float
            Subtrahend.

        Returns
        -------
        float
            Difference between arguments.
        """
        return x - y

    def multiply(self, x: float, y: float):
        """A method for multiplication that takes two arguments and returns their product.

        Parameters
        ----------
        x: float
            Multiplicand.
        y: float
            Multiplier.

        Returns
        -------
        float
            Product.
        """
        return x * y

    def divide(self, x: float, y: float):
        """A method for division.

        It takes two arguments and returns the result if the denominator is not zero,
        or raises an ValueError if the denominator is zero

        Parameters
        ----------
        x: float
            Numerator.
        y: float
            Denominator.

        Returns
        -------
        float
            Quotient.
        """
        assert y != 0, "Invalid Operation"
        return x / y

add(x, y)

A method for addition that takes two arguments and returns their sum.

Parameters:

Name Type Description Default
x float

First summand.

required
y float

Second summand.

required

Returns:

Type Description
float

Sum of the arguments.

Source code in src/calculator.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def add(self, x: float, y: float):
    """A method for addition that takes two arguments and returns their sum.

    Parameters
    ----------
    x: float
        First summand.
    y: float
        Second summand.

    Returns
    -------
    float
        Sum of the arguments.
    """
    return x + y

divide(x, y)

A method for division.

It takes two arguments and returns the result if the denominator is not zero, or raises an ValueError if the denominator is zero

Parameters:

Name Type Description Default
x float

Numerator.

required
y float

Denominator.

required

Returns:

Type Description
float

Quotient.

Source code in src/calculator.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def divide(self, x: float, y: float):
    """A method for division.

    It takes two arguments and returns the result if the denominator is not zero,
    or raises an ValueError if the denominator is zero

    Parameters
    ----------
    x: float
        Numerator.
    y: float
        Denominator.

    Returns
    -------
    float
        Quotient.
    """
    assert y != 0, "Invalid Operation"
    return x / y

multiply(x, y)

A method for multiplication that takes two arguments and returns their product.

Parameters:

Name Type Description Default
x float

Multiplicand.

required
y float

Multiplier.

required

Returns:

Type Description
float

Product.

Source code in src/calculator.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def multiply(self, x: float, y: float):
    """A method for multiplication that takes two arguments and returns their product.

    Parameters
    ----------
    x: float
        Multiplicand.
    y: float
        Multiplier.

    Returns
    -------
    float
        Product.
    """
    return x * y

subtract(x, y)

A method for subtraction that takes two arguments and returns their difference.

Parameters:

Name Type Description Default
x float

Minuend.

required
y float

Subtrahend.

required

Returns:

Type Description
float

Difference between arguments.

Source code in src/calculator.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def subtract(self, x: float, y: float):
    """A method for subtraction that takes two arguments and returns their difference.

    Parameters
    ----------
    x: float
        Minuend.
    y: float
        Subtrahend.

    Returns
    -------
    float
        Difference between arguments.
    """
    return x - y