w3resource

Python Object-Oriented Programming: Calculator class for basic arithmetic

Python OOP: Exercise-3 with Solution

Write a Python program to create a calculator class. Include methods for basic arithmetic operations.

Sample Solution:

Python Code:

# Define a class called Calculator to perform basic arithmetic operations
class Calculator:
    # Define a method for addition that takes two arguments and returns their sum
    def add(self, x, y):
        return x + y

    # Define a method for subtraction that takes two arguments and returns their difference
    def subtract(self, x, y):
        return x - y

    # Define a method for multiplication that takes two arguments and returns their product
    def multiply(self, x, y):
        return x * y

    # Define a method for division that takes two arguments and returns the result if the denominator is not zero,
    # or an error message if the denominator is zero
    def divide(self, x, y):
        if y != 0:
            return x / y
        else:
            return ("Cannot divide by zero.")

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

# Perform addition and print the result
result = calculator.add(7, 5)
print("7 + 5 =", result)

# Perform subtraction and print the result
result = calculator.subtract(34, 21)
print("34 - 21 =", result)

# Perform multiplication and print the result
result = calculator.multiply(54, 2)
print("54 * 2 =", result)

# Perform division and print the result
result = calculator.divide(144, 2)
print("144 / 2 =", result)

# Attempt to perform division by zero, which raises an error, and print the error message
result = calculator.divide(45, 0)
print("45 / 0 =", result)

Sample Output:

7 + 5 = 12
34 - 21 = 13
54 * 2 = 108
144 / 2 = 72.0
45 / 0 = Cannot divide by zero.

Explanation:

In the above exercise, we define a class called Calculator with methods add, subtract, multiply, and divide, representing basic arithmetic operations.

The add method performs addition of two numbers, subtract performs subtraction, multiply performs multiplication, and divide performs division. If the denominator is zero, the divide method returns an error message to avoid division by zero.

As an example, we create a Calculator instance, "calculator", and use it to perform various arithmetic operations.

Flowchart:

Flowchart: Python - Function that takes a sequence of numbers and determines whether all  are different from each other

Python Code Editor:

Previous: Person class with age calculation.
Next: Write a Python program to create all possible strings by using 'a', 'e', 'i', 'o', 'u'. Use the characters exactly once.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/oop/python-oop-exercise-3.php