Week 5: Python: Assignment: Exercise#2: Q1

First and foremost, I did not understand the question.

I thought I will make out by understanding the solution. But I did not understand the solution as well. Can you explain, how does the below code works, please?

def func(x):
    return x**2

def find_derivative(data):
    func, x_a = data
    delta_x = 0.000001
    f_x_a = func(x_a)
    f_delta_x = func(x_a + delta_x)
    return ((f_delta_x - f_x_a)/delta_x, x_a, f_x_a, delta_x)

data = (func, 6)
find_derivative(data)

Hi @Ashwin,
Here data is a tuple containing the function, and the integer, which is further been passed to find_derivative function.
The latter function uses a simple formula to compute the derivative for the given function and the value, and returns a tuple containing : Derivative, integer value(x), f(x), delta_x.

Thanks @Ishvinder!
I understood every word of your answer. But I am still not able to understand the solution.

Specifically, how does this work? - data = (func, 6) Is the one that we defined at the beginning? Can we pass a function as an argument?

Then how does func, x_a = data work? Why do we have to make such an assignment?

When we can call a function in another function, why is it necessary?

How did we arrive at the value delta_x = 0.000001?

Sorry, if the questions sound dumb.

  1. Yes it’s the function defined earlier.
  2. func, x_a is used to simply unwrap the tuple argument passed to the function.
  3. We’re passing func as an argument, as there can be any function with a given value, for which we may want to find derivative. In order to make the derivative function generic, it’s used like this.
  4. Delta can be any small value tending to zero.