Python Program to Create a Class which Performs Basic Calculator Operations

This Python program defines a Calculator class with four basic calculator operations

Problem Statement

You are tasked with creating a Python class that simulates a basic calculator. The class should have methods to perform common calculator operations such as addition, subtraction, multiplication, and division.

Python Program to Create a Class which Performs Basic Calculator Operations

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:
            return x / y
        else:
            raise ValueError("Cannot divide by zero")

# Create an instance of the Calculator class
calculator = Calculator()

# Perform operations
print("Addition:", calculator.add(10, 5))
print("Subtraction:", calculator.subtract(10, 5))
print("Multiplication:", calculator.multiply(10, 5))
print("Division:", calculator.divide(10, 5))

How it Works

  1. Class Definition: The BasicCalculator class is defined with four methods: add, subtract, multiply, and divide. Each method takes two arguments (x and y) representing the numbers on which the corresponding operation should be performed. The methods are placeholders for you to implement the actual operations.
  2. Method Implementation:
    • add(self, x, y): This method should calculate the sum of the two input numbers x and y and return the result.
    • subtract(self, x, y): This method should calculate the difference between the two input numbers x and y and return the result.
    • multiply(self, x, y): This method should calculate the product of the two input numbers x and y and return the result.
    • divide(self, x, y): This method should calculate the division of the dividend x by the divisor y. If y is not zero, it should return the result. However, if y is zero, it should raise a ValueError to indicate that division by zero is not allowed.
  3. Usage Example:
    • You create an instance of the BasicCalculator class, such as calculator = BasicCalculator().
    • You can then use the instance’s methods to perform calculator operations.
  4. Output:
    • The actual output will depend on the values you provide as arguments for the operations. For example, if you call add(10, 5), the result will be 15. If you call divide(10, 0), it will raise a ValueError with a message indicating division by zero is not allowed.

Input/ Output

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...