statements and expressions

Chris Liechti cliechti at
Wed Jan 9 17:36:58 EST 2002

"Alves, Carlos Alberto - Coelce" <calves at> wrote in
news:mailman.1010608272.28800.python-list at 

> Why this two codes doesn't work?!
> 1º case:
>      exec("print 'Carlos' ")
> 2º case:
>      x = exec("something")
> In former case, is it beacouse exec(..) requires an expression as
> argument and print "...." is a statement? And in the latter, beacouse
> statements doesn't support assignments?!
> Thanks in advance,

in your case both fail because you use exec instead of eval...

do the following examples help?

>>> something = "hello"
>>> x = eval("something")
>>> x
>>> exec "print 'hello'"

eval is a function and needs an expression.
exec is a statement and executes arbitrary python code but does not return 
anything as it is no function.


Chris <cliechti at>

More information about the Python-list mailing list