Home » eval() in Python | Python eval() Function with Examples

eval() in Python | Python eval() Function with Examples

by Online Tutorials Library

Python eval() Function

The python eval() function parses the expression passed to it and runs python expression(code) within the program.

Signature

Parameters

expression – The string is parsed and evaluated as a Python expression.

globals (optional) – A dictionary that specifies the available global methods and variables.

locals (optional) – It is an another dictionary which is commonly used for mapping type in Python.

Return

It returns the result evaluated from the expression.

Python eval() Function Example 1

The below example shows the working of eval():

Output:

6  

Explanation: In the above example, we take an integer variable named x, and eval() function parses the expression and returns result evaluated from the expression.


Next TopicPython Functions

You may also like