Solution Review: Average of a List
This lesson will explain the solution to the exercise in the previous lesson.
We'll cover the following...
Solution
Press + to interact
# function to compute average of a listdef average(input_list):# sum calculationsum_list = 0for i in input_list:sum_list = sum_list + i# average calculationavg = sum_list /len(input_list)return avgsample_list = [2,4,5,1]avg_list = average(sample_list)print(avg_list)
We define the average
function in lines 2-8. Let’s break this function down. For computing the average of the list, we need to find the sum of the numbers in the list and the length of the list. We already know we can use the len
function to compute ...