Why do we create another variance variable after the function?



Why do we create another variance variable after the function?


In order to use the functions we just made, we need to give them values. We have the list, grades, to pass our other functions, but the function to calculate standard deviation expects a variance value to be provided, which we don’t have anywhere.
It may seem confusing because we do have a variable named variance inside of the grades_variance() and grades_std_deviation() functions, but those variables only exist inside of those functions. This is called scope of a variable, or where it is accessible.
Think of the stuff inside of functions as only being accessible inside of that function, which is why return is so useful.

FAQ: Learn Python - Exam Statistics - Standard Deviation

Why do we create a variable outside of the scope of the functions this time?

All other functions we were tasked with creating handle everything within their scope. I understand the benefit of having the data accessible by calling a variable instead of the function. However, why haven’t we done that for all of the previous functions as well? I imagine that data is just as important to statistics.


I agree, but would put it the other way round:
We always called the helper functions inside the new function.
And we got our result by calling a function with grades as args.

Wouldn’t it be more logical to follow through with this pattern, giving std_dev scores as param and having it call our_variance = variance(scores), then return our_variance ** 0.5?