module3 module¶
- class module3.Calculator(name='Simple Calculator')¶
Bases:
object
A simple calculator class to perform basic arithmetic operations.
This class provides methods for basic arithmetic operations including addition, subtraction, multiplication, and division.
- name¶
The name of the calculator.
- Type:
str
Examples
>>> calc = Calculator("My Calculator") >>> calc.add(5, 3) 8.0 >>> calc.divide(10, 2) 5.0
- add(a, b)¶
Return the sum of two numbers.
- Parameters:
a (float) – The first number.
b (float) – The second number.
- Returns:
The sum of a and b.
- Return type:
float
Examples
>>> calc = Calculator() >>> calc.add(2, 3) 5.0
- divide(a, b)¶
Return the division of two numbers.
- Parameters:
a (float) – The numerator.
b (float) – The denominator.
- Returns:
The quotient of a divided by b.
- Return type:
float
- Raises:
ValueError – If the denominator is zero.
Examples
>>> calc = Calculator() >>> calc.divide(8, 2) 4.0 >>> calc.divide(5, 0) Traceback (most recent call last): ... ValueError: Denominator cannot be zero.
- multiply(a, b)¶
Return the product of two numbers.
- Parameters:
a (float) – The first number.
b (float) – The second number.
- Returns:
The product of a and b.
- Return type:
float
Examples
>>> calc = Calculator() >>> calc.multiply(4, 2.5) 10.0
- subtract(a, b)¶
Return the difference between two numbers.
- Parameters:
a (float) – The first number.
b (float) – The second number.
- Returns:
The difference of a and b.
- Return type:
float
Examples
>>> calc = Calculator() >>> calc.subtract(10, 5) 5.0