Basic Usage
This notebook demonstrates the basic usage of the Calculator package.
The basic arithmetic operations are contained in the Calculator class.
[1]:
from calculator import Calculator
[2]:
calc = Calculator()
The subsequent sections operate on two numbers \(a\) and \(b\).
[3]:
a = 5
b = 3
Addition
The addition operation add both numbers a and b
\(\Large c = a + b\)
[4]:
c = calc.add(a, b)
[5]:
print(f"The result of the addition is {c}.")
The result of the addition is 8.
Subtraction
The subtraction operation subtracts number \(b\) from number \(a\)
\(\Large c = a - b\)
[6]:
c = calc.subtract(a, b)
[7]:
print(f"The result of the subtraction is {c}.")
The result of the subtraction is 2.
Multiplication
The multiplication operation multiplies the fatctors \(a\) and \(b\)
\(\Large c = a \cdot b\)
[8]:
c = calc.multiply(a, b)
[9]:
print(f"The result of the multiplication is {c}.")
The result of the multiplication is 15.
Division
The division operation divides \(a\) by \(b\)
\(\Large c = \frac{a}{b}\)
[10]:
c = calc.divide(a, b)
[11]:
print(f"The result of the division is approximately {c:.3f}.")
The result of the division is approximately 1.667.
Note: You must not divide by \(0\).
[12]:
c = calc.divide(a, 0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[12], line 1
----> 1 c = calc.divide(a, 0)
File /mnt/c/Users/sebas/physics/repos/sphinx_demo/calculator/calculator.py:100, in Calculator.divide(self, a, b)
98 raise TypeError("Inputs have to be numbers!")
99 if b == 0:
--> 100 raise ValueError("Divisor must not be zero!")
101 return a / b
ValueError: Divisor must not be zero!