operators as variables

Terry Reedy tjreedy at udel.edu
Sat Dec 6 19:49:47 CET 2008

macc_200 wrote:
> Hi,
> just starting programming and have an elementary question after playing 
> around with lists but cannot find the answer with googling.
> I have a list of variables and I would like some of those variables to 
> be integers and some to be operators so the list would look something 
> like [5 * 4 - 4 + 6] and then be able to evaluate the result (i.e. get 
> 10).  How do you make the interpreter see the operator as that instead 
> of a string and just echo the list back to me.

I am not sure what you actually want to do, but the following may help:

1. Strings can be evaluated.
2. Operators are syntax sugar for functions.  The functions are 
available in the operator module.

IDLE 3.0
 >>> eval('5 * 4 - 4 + 6')
 >>> import operator as op
 >>> op.add(2,3)


More information about the Python-list mailing list