Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python eval

#Perfect if you want to store a mathematical expression without defining the
#variables used:
my_function = "5x**2 + 8x - 2"  #I want to define the function cleanly as a
                                #   variable
#insert definition of other varianles here
#insert other code here
def f(x):                       #Defining the function as the funtion f(x)                  
  return eval(my_function)    #Returning my_function with the x in f(x) as x

print(f(0))  #Printing f(0), thereby returning my_function with 0 as x
#Returns -2
Source by www.mygreatlearning.com #
 
PREVIOUS NEXT
Tagged: #python #eval
ADD COMMENT
Topic
Name
9+7 =