Home  >  Article  >  Backend Development  >  Does python have an eval function?

Does python have an eval function?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-19 11:15:142147browse

Does python have an eval function?

Python has the eval function, the relevant introduction is as follows:

Description

eval() function is used to execute a string expression and return the value of the expression.

Grammar

The following is the syntax of the eval() method:

eval(expression[, globals[, locals]])

Related recommendations: "python video tutorial"

Parameters

expression -- Expression.

globals -- Variable scope, global namespace, if provided, must be a dictionary object.

locals -- Variable scope, local namespace, if provided, can be any mapping object.

Return value

Returns the expression calculation result.

Example

The following shows an example of using the eval() method:

>>>x = 7
>>> eval( '3 * x' )
21
>>> eval('pow(2,2)')
4
>>> eval('2 + 2')
4
>>> n=81
>>> eval("n + 4")
85

The above is the detailed content of Does python have an eval function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn