Introduction to Python eval()
Eval() function is a built-in function of the Python Programming Language. The eval() method/function parses the expression argument/arguments which are passed to the method and runs the expression of python(python code) with the program/program statements. In common terms, the eval() method/methods which run the python programming code (which is actually passed as the argument) within the program. In other words, we can also say eval() function evaluates the “String” like Python Code expression and then returns of the string as an integer.
Syntax:
eval(expression, globals=None, locals=None)
Explanation:
Eval() function usually needs 3 parameters but the 2nd and 3rd parameter terms are optional/not much important than the 1st parameter. The 3 parameters are expression, globals, and locals.
- Expression parameter: This is the string that is as parsed and also evaluated as the Python expression.
- Globals parameter ( dictionary parameter ): This parameter is a dictionary and this parameter is also optional. Nothing happens even if we don’t specify this parameter in the eval() function.
- Locals parameter ( Mapping Object ): This locals parameter is an object which is mapping.. The dict or dictionary of the globals parameter is the best and standard mapping type in the Python Programming Language commonly.
How Python Eval Function Work with Examples
Eval() function/method will returns the value/result after evaluating the expression. eval function works by parsing the string value and that string value also be evaluated as the python expression.
Example #1
In the below example of the syntax, a variable “x” is created and assigned value “10” to it. Then a print statement is made to show the value of x using the eval () function. Then after that “value of x+10.==>” will be printed after that addition of the value of x and the value “10” will be printed by using the eval() function. Then “value of x^2==>” will be printed. Then the square of the value x which is square of “10” will be printed by multiplying the x value with the value of x itself inside of the eval() function. Then “value of x^3==>” will be printed using the print() function. Then the value of x is multiplied with the value of x 3 times using the eval() function and then the cube of x’s value will be printed.
Now the IF and ELSE conditions are prepared to make the prime number logic program by using the value of x. We all know that the prime number is the only number which is divided by the number ‘1” and the number itself. Here in the below program at first IF, if the value x is divided by the value 2 and the interpreter goes to the next IF condition to check whether the value of x is exactly divided by the value 3. If the value of x divided by 3 doesn’t leaves remainder 0 then the python interpreter goes to the next IF condition and checks whether the x value divided by the value 7 and checks whether the value of x divides by 7 leaves 0 or not but here the value of x not exactly divided so the Python Programming language’s interpreter goes to the ELSE conditions. In the ELSE condition, print statements will print like “x is not a prime number”.
At each and every IF statements, if the IF condition is False then the interpreter of python goes to the ELSE condition’s statements and the programming inside of it will be printed/interpreted etc.. all at a time but the program inside of the ELSE condition is also be interpreted every line one by one.
4.8 (7,888 ratings)
View Course
Code:
x = 10
print ('value of x ==>')
print(eval('x'))
print ('value of x+10 ==>')
print(eval('x+10'))
print ('value of x^2 ==>')
print(eval('x*x'))
print('value of x^3==>')
print(eval('x*x*x'))
if(x%2==0):
print('X is even number')
if(x%2!=0):
if(x%3!=0):
if(x%5!=0):
if(x%7!=0):
print(eval('x'))
print('is a prime number')
else:
print ('x is not prime number')
else:
print('x is not prime number')
else:
print('x is not prime number')
else:
print('x is not prime number')
Output:
Example #2
In the below example of programming code, the math library is introduced and imported all of the math functions using the * symbol. Then a for loop is made with the initialization value “1” with the incrementation “1” at a time upto the value “3” from the value “1”. Then inside the for loop, a func1 variable is created with the input function with the string value “Enter the Math Function which is mentioned below to Evaluate” then the interpreter go the next line and prints “Functions Allowed are: 1. Square_root(x) and 2. Power(x,y)” then goes to the next line to print/interpret the remaining python code.
Now we enter the try, except concept. These are just like the IF and ELSE condition. If the “try” has correct parameters then try will run properly and exits from the program or else the except concept come into the picture. Try, except concept will be very much helpful to handle errors along with usage of the eval() function/functions. Statements that are in the try will print the eval() function which has math functions “square root” and the power of x and y values which are about to declare manually by the user. If the input doesn’t contain any input as needed with the mentioned math functions, the result will be printed and the loop once again runs and asks again for the input or else you can just enter and go the except concept. There you will print the ext1 value and interpreter breaks out and comes out from the except concept. Then the print statement will print “Now it is Done” like that.
Code:
from math import *
for lm in range(1, 3):
func1 = input("Enter the Math Function which are mentioned below to Evaluate.\nFunctions Allowed are: 1. square_root(x) and 2. power(x,y):\n")
try:
print(eval(func1, {'square_root': sqrt, 'power': pow}))
except Exception as ex1:
print(ex1)
break
print('Now it is Done')
Output:
Recommended Articles
This is a guide to Python eval(). Here we discuss the introduction and working of python eval() function along with different examples and its code implementation. You may also look at the following articles to learn more –