Javatpoint Logo
Javatpoint Logo

Python eval() Function

Python is a flexible programming language with a vast library of functions that can make coding a lot more straightforward. Quite possibly of the most remarkable and frequently used function in Python is the eval() function. In this article, we will examine the eval() function exhaustively and figure out how to use it in Python.

What is the eval() function?

The eval() function is an underlying Python function that evaluates a string as a Python expression. It takes a single parameter, which is a string that addresses a Python expression. The eval() function then evaluates this expression and returns the outcome. The python eval() function parses the expression passed to it and runs python expression(code) within the program.

The eval() function is in many cases used in Python to execute dynamically produced code. For instance, on the off chance that you have a string that contains a numerical expression, you can use the eval() function to evaluate that expression and return the outcome.

Syntax:

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():

Code

Output:

40

Explanation:

In this example, we have defined two variables x and y and introduced them with the values 15 and 25, separately. We then, at that point, call the eval() function with the string 'x + y' as the expression. The eval() function evaluates this expression as the sum of the values of x and y, which is 40. At last, we print the outcome using the print() function.

Python eval() Function Example 2

As well as accepting a single parameter that addresses a Python expression as a string, the eval() function likewise permits us to determine the context where the expression ought to be evaluated. You can use the locals and globals contentions to pass in dictionaries references that address the local and global namespaces, separately.

Here is an example that shows the way that you can use locals and globals with the eval() function:

Code

Output:

20

Explanation:

In this example, we have defined two variables x and y, and a string expr that contains a Python expression. We then call the eval() function with the globals() and locals() functions as contentions, which address the global and local namespaces, separately. The eval() function evaluates the expression 'x + y' utilizing the globals() and locals() namespaces and returns the outcome, which is 20.

Python eval() Function Example 3

We can likewise use the locals() and globals() dictionaries to assign values to variables in the expression that you are evaluating. Here is an example:

Code

Output:

100

Explanation:=

In this example, we have defined two variables x and y, a string expr that contains a Python expression, and a dictionary reference that contains the worth of the variable z. We then, at that point, call the eval() function with the globals() and locals() functions as contentions. The eval() function evaluates the expression 'x + y + z' utilizing the globals() and locals() namespaces and returns the outcome, which is 100.

Utilizing locals() and globals() with the eval() function can be useful when you want to evaluate an expression in a particular context, like a function or class. Be that as it may, it means quite a bit to use these contentions with caution, as they can permit us to change variables unexpectedly.

Conclusion:

The eval() function is an amazing asset in Python that permits us to execute dynamically produced code and perform complex numerical computations. In many cases, it is best to use the eval() function with caution and know about its possible security, execution, and maintenance issues.


Next TopicPython Functions





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA