← Go Back

eval() Built-in Function

The eval() built-in function is similar to exec() (see How to Execute Python Code Contained in a String), but instead of executing any Python code contained in a string, it only accepts expressions (that is, portions of code with a result), and returns the result of the evaluated expression.

>>> day = "Thursday"
>>> eval("day == 'Friday'")
False

Note that the result (in this case, False) is a Python object, not a string. If you try to evaluate code that is not an expression, the function throws an error:

>>> eval("pi = 3.14")
File "<string>", line 1
pi = 3.14
^
SyntaxError: invalid syntax


compilation built-ins


🐍 You might also find interesting: