Averaging the Values in a List in Python:-
The first step in calculating the average of the values in a list is to get the total of the values. The second step is to divide the total by the number of elements in the list. Program (given below) demonstrates the algorithm.
(average-list.py)
# This program calculates the average of the values in a list #.
def main ( ):
# Create a list.
scores = [2.5, 8.3, 6.5, 4.0, 5.2]
# Create a variable to use as an accumulator.
total = 0.0
# Calculate the total of the list elements.
for value in scores:
total += value
# Calculate the average of the elements.
average = total / len(scores)
# Display the total of the list elements.
print 'The average of the elements is', average
# Call the main function.
main ( )
Output
The average of the elements is 5.3


LinkBack URL
About LinkBacks
Reply With Quote

LinkBacks Enabled by vBSEO
Bookmarks