Tuples

Python provides a way to get both the value of the function and its derivative in a single function call. We simply compute and return both values as follows:

############################################################                    
def f_and_df(x):
    """Evaluate the function and its derivative"""
    y = 4*x - math.cos(x)
    dydx = 4 + math.sin(x)
    return y, dydx

Then in the main loop of the code, we would write, for example

    f, dfdx = f_and_df(x)
    pnew = p - f/dfdx

The assignment to f and dfdx is done term by term, in the order written, just as we want.

A bunch of comma-separated values is called a “tuple” in Python lingo. They can be written inside parentheses. Tuples can have any number of values, including none.

Note that the quadratic.py example also used a tuple:

a, b, c = eval(input("Enter a, b, c "))

In this case if the user enters three comma-separated values, the eval function returns a tuple of three values.