Python eval() built-in function

From the Python 3 documentation

The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can be any mapping object.

Introduction

The eval() function in Python is a built-in function that parses a string as a Python expression and evaluates it. It can be used to execute arbitrary Python code from a string, which can be both powerful and risky. It’s often used in situations where you need to evaluate dynamically generated expressions, but it should be used with caution due to potential security vulnerabilities.

Examples

>>> eval('1 + 4')
# 5

>>> eval('print("Hello World!")')
# Hello World!

>>> x = 10
>>> eval('x == 10')
# True

The eval() function can also be used with the print() function to display output to the console.

Subscribe to pythoncheatsheet.org

Join 16,702+ Python developers in a two times a month and bullshit free publication , full of interesting, relevant links.