Because x=1 is an expression, not an expression. Use exec to run statements.
>>> exec('x=1') >>> x 1
By the way, there are many ways to avoid using exec / eval if all you need is a dynamic name for assignment, for example. you can use the setattr function dictionary, or locals() dictionary :
>>> locals()['y'] = 1 >>> y 1
Update . Although the above code works in REPL, it will not work inside a function. See Changing Locales in Python for some alternatives if exec out of the question.
kennytm Apr 08 2018-11-11T00: 00Z
source share