eval() and exec()

Return value

Eval function evaluates the python code and returns the value. Print the value of function return

Exec execute the code and always returns None

Number of expression

Eval only evaluates the single expression

Exec can be used to execute any number of expression

Assignment operator

Exec can be used to assign a value to variable, but eval will throw error

Conclusion

Eval is for expression and returns the value of expression. Exec is for statement and return ‘None’

>>> a = 5
>>> eval('37 + a')   # it is an expression
42
>>> exec('37 + a')   # it is an expression statement; value is ignored (None is returned)
>>> exec('a = 47')   # modify a global variable as a side effect
>>> a
SuperMade with Super