49

Usual method of applying mathematics to variables is

a * b

Is it able to calculate and manipulate two operands like this?

a = input('enter a value')
b = input('enter a value') 
op = raw_input('enter a operand')

Then how do i connect op and two variables a and b?
I know I can compare op to +, -, %, $ and then assign and compute....

But can i do something like a op b, how to tell compiler that op is an operator?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Abhilash Muthuraj
  • 2,008
  • 9
  • 34
  • 48

5 Answers5

90

You can use the operator module and a dictionary:

import operator
ops = {
    "+": operator.add,
    "-": operator.sub,
    "*": operator.mul,
    "/": operator.div
}   
op_char = input('enter a operand')
op_func = ops[op_char]
result = op_func(a, b)
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 5
    For python 3.x the `operator.truediv` or `operator.floordiv` should be used instead https://docs.python.org/3/library/operator.html – joni jones Feb 14 '17 at 21:20
  • additionally, using op_func in this way doesn't work. As the edit queue on this answer is full I've fixed these errors in an answer below (assuming this answer remains ACCEPTED w/ 76 upvotes...) – Robert Houghton Dec 31 '21 at 17:12
  • @RobertHoughton: what do you claim "doesn't work" with this answer? (I don't know what you mean by "the edit queue" either, but that's maybe just something I don't see.) – rici Dec 31 '21 at 20:06
11

The operator module http://docs.python.org/library/operator.html exposes functions corresponding to practically all Python operators. You can map operator symbols to those functions to retrieve the proper function, then assign it to your op variable and compute op(a, b).

Luciano Ramalho
  • 1,981
  • 18
  • 22
5

I know this is a really old thread, but I believe at the time people didn't know about the eval function (Maybe it came with Python 3). So here's an updated answer to the question

a = input('enter a value')
b = input('enter a value') 
op = input('enter an operand')
expression = a + op + b # simple string concatenation
result = eval(expression)

If the input is not expected to be valid all the time ast.literal_eval can be used instead. It raises an exception if the input isn't a valid Python datatype, so the code won't be executed if it's not. Eg. if a, b and op are respectively 5, 10, + then result is 15

Eeshaan
  • 1,557
  • 1
  • 10
  • 22
  • 2
    It should be expression = a + c + b . btw, using eval for user input is a bad idea. ast.literal_eval is better. – SilentGuy Oct 23 '20 at 22:20
  • @SilentGuy you're very much right! I've updated my answer – Eeshaan Oct 24 '20 at 03:41
  • 5
    `eval` wasn't new to Python 3. The reason people aren't suggesting it is because it is not safe to use on user input. It can execute any Python expression (with any side effects) that someone types in. Also, `literal_eval` only evaluates literals. It cannot evaluate an arithmetic expression like `'5+10'`. – khelwood May 16 '21 at 13:03
  • `ast.literal_eval` cannot do arithmetic in general, or deal with other operators. It can only handle `+` and `-` [because of a quirk in the Python grammar](https://stackoverflow.com/questions/40584417/why-does-ast-literal-eval5-7-fail). – Karl Knechtel May 29 '22 at 23:57
1

You'll need to compare the user's inputted string to your list of operands by hand. There is no analogue of int() here, since operators are keywords in the language and not values.

Once you've compared that input string to your list of operands and determined the operator that it corresponds to, you can use the Python standard library's operator module to calculate the result of applying the operator to your two operands.

Jordan Lewis
  • 16,900
  • 4
  • 29
  • 46
1

You can use the operator module to create a dictionary. From the Python 3 documentation of that module:

"The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python. For example, operator.add(x, y) is equivalent to the expression x+y."

import operator
ops = {
    "+": operator.add,
    "-": operator.sub,
    "*": operator.mul,
    "/": operator.truediv
}   
op_char = input('enter a operand')
a = 1
b = 2
result = ops[op_char](a,b)
print(result)
Robert Houghton
  • 1,202
  • 16
  • 28