In this python built-in tutorial, you will learn all about the Python eval function to evaluate the expression, If the expression is a valid python statement.
In our previous python tutorials, we have seen Python enumerate function to add a counter as the key of enumerate object.
Headings of Contents
Python eval() function.
Python eval function is a built-in function that is used to evaluate the expression if the expression is a legal statement.
Syntax
The syntax of eval function in Python is:-
eval(expression, globals, locals)
Parameters
The eval function accepts three parameters.
- expressions:- A String, that will be evaluated as Python code.
- globals:– Optional. A dictionary containing global parameters.
- locals:- Optional. A dictionary containing local parameters.
Python eval function examples
In this example, we will use the python eval method to evaluate the valid python statement.
Example 1:
x = 'print("Welcome to Python")'
eval(x)
Output will be:- Welcome to Python
Example 2:
x = 12
y = 10
y = 2
result = eval('x + y % y ')
print(result)
Output will be :- 11
Conclusion
So, In this tutorial, we have seen the python eval function to evaluate the expression, If the expression is a valid Python statement.
You can also execute simple arithmetic operations, using the eval function in Python. If this article helped you, please keep visiting for further python built-in functions tutorials.
Other Python built-in functions
For more information:- Click Here