To do "right" is something that depends on how much functionality you want, and it is something that, if you want to read parentheses, can be very complicated.
But to keep it simple, let's assume that the input expressions are always in the form number, operator, number, and everything separated by spaces - and also let Python itself process numeric operands to give its value as a number floating. (that is, let's call the built-in "float" instead of processing the digit-by-digit input to construct the operands)
So we focus only on the operator itself - and we can create a simple dictionary that maps the operator's symbol to a function that receives two parameters. To get shorter still, we can use the keyword
lambda
that allows the definition of simple functions as expressions, within the dictionary body itself.
Then you can do something like this:
operadores = {
"+": lambda op1, op2: op1 + op2,
"-": lambda op1, op2: op1 - op2,
"*": lambda op1, op2: op1 * op2,
"/": lambda op1, op2: op1 / op2
}
def process(expression):
op1, operator, op2 =expression.split()
op1 = float(op1)
op2 = float(op2)
return operadores[operator](op1, op2)