I thought this would print 3, but it prints 1:
def f(): a = 1 exec(\"a = 3\") print(a)
If you are inside a method, you can do so:
class Thing(): def __init__(self): exec('self.foo = 2') x = Thing() print(x.foo)
You can read more about it here