Not a necessity, at all. The return statement is just fine.
Style guides almost universally agree with avoidance using lowercase L as variable. It is not easy to distinguish from a numeral, 1.
But this is Statistics and we do have a variable, n, a counting number to indicate number of terms in the sample space.
n = len(scores)
When we get right down to it, we don't use n anywhere but the return statement, so don't need a variable at all.
return variance / len(scores)
I've already mentioned in a recent topic on this unit that the variable, variance is possibly not the best choice on the part of the course author. The return value (a rational) is the variance; the sum of the squares is not.
mean = grades_average(scores)
sumx = 0
for x in scores:
sumx += (x - mean) ** 2
return sumx / len(scores)
A little revision gives us a readable and simple function that has only working variables, one static, one dynamic. Think of unnecessary variables as being like the phrase your teacher says made the paragraph more difficult to read and disseminate.
When you get to advanced topics at the end of the track you will learn a new construct that is very powerful for instances such as this. This is only a demo, so don't use it in your submitted code. Stick to the algorithms you are currently working with.
u = grades_average(scores)
return sum([(x - u) ** 2 for x in scores]) / len(scores)
print grades_variance(grades) # 334.071005917