Skip to content

Python eval() function

  • by

Python eval() function is used to evaluate a passed expression and runs python expression (code) within the program. The eval() method returns the result evaluated from the expression.

eval(expression, globals, locals)  

Python eval() function

A simple example code evaluates the specified expression.

x = 1
print(eval('x + 1'))

res = eval("sum([8, 16, 32])")
print(res)

Output:

Python eval() function

Do comment if you have any doubts or suggestions on this Python function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading