Basic calculator that calculates result based on operands using a class Question:


  Create a basic calculator using classes that given 3 inputs - number 1, number 2 and operator -  calculates the result. 


class calculator():

    def __init__(self,value1 , value2, operand):
        self.value1 = int(value1)
        self.value2 = int(value2)
        self.operand = operand

    def calculate (self):
        if self.operand == '+' :
            return self.value1 + self.value2

        if self.operand == '*':
            return self.value1 * self.value2

        if self.operand == '-' :
            return self.value1 - self.value2

        if self.operand == '/' :
            if self.value2 == 0:
                raise ZeroDivisionError
            else:
                return self.value1 / self.value2


r = calculator(6,7, '+')
print r.calculate()

r = calculator(3,7, '-')
print r.calculate()

r = calculator(6,0, '*')
print r.calculate()

r = calculator(9,5, '/')
print r.calculate()


How can i make this function better? Is this a correct implementation of a class?
There is no need to manually raise ZeroDivisionError. Python will do it for you anyway.
Rather than inspecting an operand with the bunch of ifs, create a dictionary of operand, lambda:

ops = {
    '+': lambda x,y: x + y,
    ....
}


and calculate the result as

ops[operand](value1, value2)


Such dictionary (initialized in a constructor) is the only justification for making a class. It doesn't feel right to make an operator and values class members. Consider

r = calculator()
r.calculate(1, 2, '+')
r.calculate(3, 4, '*')


etc.