Python class and object

Author: Al-mamun Sarkar Date: 2020-03-25 18:06:24

Python class and object. The following code shows how to make a class and create object of that class and calling method using the object.

Code:

class Calculator:
    """Do addition, subtraction, multiplication and division."""

    def addition(self, a, b):
        return a+b

    def subtraction(self, a, b):
        return a-b

    def multiplication(self, a, b):
        return a*b

    def division(self, a, b):
        try:
            return a/b
        except ZeroDivisionError:
            return 'It is impossible to divide by zero.'


my_calculator = Calculator()

temp = my_calculator.addition(12, 78)
print(temp)

temp = my_calculator.subtraction(50, 23)
print(temp)

temp = my_calculator.multiplication(9, 19)
print(temp)

temp = my_calculator.division(400, 5)
print(temp)

temp = my_calculator.division(43, 0)
print(temp)

 

Output:

90
27
171
80.0
It is impossible to divide by zero.