85 lines
2.2 KiB
Python
85 lines
2.2 KiB
Python
# sample.py
|
|
|
|
class Calculator:
|
|
"""
|
|
A kk kk kk simple calculator class to perform basic arithmetic operations.
|
|
"""
|
|
|
|
def __init__(self, value=0):
|
|
"""
|
|
Initializes the calculator with a starting value.
|
|
|
|
:param value: The starting value of the calculator (default is 0).
|
|
"""
|
|
self.value = value
|
|
|
|
def add(self, number):
|
|
"""
|
|
Adds a number to the current value.
|
|
|
|
:param number: The number to add.
|
|
:return: The updated value after addition.
|
|
"""
|
|
self.value += number
|
|
return self.value
|
|
|
|
def subtract(self, number):
|
|
"""
|
|
Subtracts a number from the current value.
|
|
|
|
:param number: The number to subtract.
|
|
:return: The updated value after subtraction.
|
|
"""
|
|
self.value -= number
|
|
return self.value
|
|
|
|
def multiply(self, number):
|
|
"""
|
|
Multiplies the current value by a given number.
|
|
|
|
:param number: The number to multiply with.
|
|
:return: The updated value after multiplication.
|
|
"""
|
|
self.value *= number
|
|
return self.value
|
|
|
|
def divide(self, number):
|
|
"""
|
|
Divides the current value by the given number.
|
|
|
|
:param number: The number to divide by.
|
|
:return: The updated value after division.
|
|
:raises ZeroDivisionError: If the number is zero.
|
|
"""
|
|
if number == 0:
|
|
raise ZeroDivisionError("Cannot divide by zero.")
|
|
self.value /= number
|
|
return self.value
|
|
|
|
def clear(self):
|
|
"""
|
|
Resets the calculator to its initial value (0).
|
|
|
|
:return: The reset value (0).
|
|
"""
|
|
self.value = 0
|
|
return self.value
|
|
|
|
def main():
|
|
"""
|
|
Main function to demonstrate usage of the Calculator class.
|
|
|
|
Creates an instance of the Calculator and performs a series of operations.
|
|
"""
|
|
calc = Calculator()
|
|
print(f"Initial value: {calc.value}")
|
|
print(f"Add 10: {calc.add(10)}")
|
|
print(f"Subtract 5: {calc.subtract(5)}")
|
|
print(f"Multiply by 2: {calc.multiply(2)}")
|
|
print(f"Divide by 5: {calc.divide(5)}")
|
|
calc.clear()
|
|
print(f"After clear: {calc.value}")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|