動的実行には、複数の式を実行する exec がある。

exec("""
total = 0
for i in range(5):
 print(i)
 total += i

print(total)
""")

これを利用すれば、プログラムを変更しないで外からの入力で動作を変えたりできる。