try:
    as_float = float(symbol)
except ValueError:
    raise ValueError('Invalid symbol or number')
else:
    if as_float.is_integer():
        return NumericOperand(int(as_float))
return NumericOperand(as_float)
How to parse a number as either an int or a float, depending on required precision?