eval inpython code example

Example 1: python eval function

# eval(expression, [globals[, locals]]
x = 100 
y = 200 # x and y are global vars
eval('6 * 7')
# output : 42
eval('x + y')
#output : 300

# we can override global vars x and y as follows
eval('x + y',{'x':50,'y':25})
#output: 75 and not 300
     
'''eval accepts the following expression examples:
literals : 6 and 7 from above are literals also lists, floats, boolean,strings
names : my_num = 3 # the my_num is the name :NOTE that assignments are not allowed in eval
attributes: funtion_name.attribute
operations: * from above is an operator
functions: must return a value eval( sum( [8,16,32] ) ) gives: 56, fibonacci(3)
'''

Example 2: eval in python

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