Creating a basic calculator for integers Create a simple function that given 3 inputs - number 1, number 2 and operator -  calculates the result. The function supports DMAS operations, and accepts only integers


Is there a better way of solving this problem?

#calculator
def calculate(x , y , op):
    if op == '+' :
        z = x  + y
    elif op == '-' :
        z = x - y
    elif op == '*' :
        z = x * y
    elif op == '//' :
        if y!=0:
            z = x // y
        else:
            z = "undefined"
    return z

print calculate(9 , 9 , '//')
print calculate(11 , 0 , '//')
Make it robust

Your solution could be a lot more robust. For example, you don't currently validate that x or y are integers - you should check, and if they aren't you should reject them. Additionally, you might want to do some string manipulation of the operator (for example, strip spaces). 

It could also be useful to not take strings, but instead create an enumeration (Python 3.4+ Python 3.4-). I'd also advise that you look at the linked stack overflow post from the comments (here for posterity). That makes this into a trivial function, where the only work is in your lookup table.

Use exceptions

Your prompt doesn't define what should happen if you get 0 as the denominator; I'd throw an exception (or if you're using operator let it handle it for you) instead of returning "undefined", as it is much more surprising to get this:

&gt;&gt;&gt; calculate(9, 0, "//") + 2
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: must be str, not int


Than it is to get this:

&gt;&gt;&gt; calculate(9, 0, "//") + 2
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "&lt;stdin&gt;", line 1, in calculate
ZeroDivisionError: division by zero


Additionally, by throwing the expected error other applications that may know how to handle a divide-by-zero event will "just work"TM with your module.

Format your code

Instead of the comment #calculator right above the function definition, which tells us literally nothing, add a docstring that defines inputs, outputs, and purpose of the function.

Additionally, your spacing is a little weird; look up PEP8 and see what it recommends.