I have to evaluate an expression, which uses operators that are stored in variables.
item1 = "*"
item2 = "+"
item3 = "/"
item4 = "-"
suppose I want to compute the value of 4*3-5/8, how could I do this with my string variables ?
user1251007
17k14 gold badges52 silver badges78 bronze badges
asked Jan 7, 2014 at 23:21
mounaim
1,1788 gold badges34 silver badges63 bronze badges
-
Read about eval.BartoszKP– BartoszKP2014年01月07日 23:33:25 +00:00Commented Jan 7, 2014 at 23:33
1 Answer 1
Use eval:
>>> eval('5+4')
Out[15]: 9
>>> eval('5*4')
Out[16]: 20
>>> eval('5-4')
Out[17]: 1
answered Jan 7, 2014 at 23:54
Steinar Lima
7,8292 gold badges41 silver badges39 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
mounaim
Thanks BartoszKP and Steinar Lima, this what I was looking for :)
lang-py