In Python Programming Language , a class is a blueprint for creating objects . It defines the attributes and behaviours that the objects will have.
Basic calculator consists of operations like addition, subtraction, multiplication, division.
class Calculator:
def add(self, x, y):
return x + y
def subtract(self, x, y):
return x - y
def multiply(self, x, y):
return x * y
def divide(self, x, y):
if y == 0:
raise ValueError("Cannot divide by zero.")
return x / y
if __name__ == "__main__":
calc = Calculator()
print("Addition: 6 + 5 =", calc.add(6, 5))
print("Subtraction: 6 - 5 =", calc.subtract(6, 5))
print("Multiplication: 6 * 5 =", calc.multiply(6, 5))
print("Division: 6 / 5 =", calc.divide(6, 5))
Output
Addition: 6 + 5 = 11 Subtraction: 6 - 5 = 1 Multiplication: 6 * 5 = 30 Division: 6 / 5 = 1.2