def print_grades(grades): for index, grade in enumerate(grades): print "{:3d}. : {:5.1f}".format(index, grade) ## formatting does not really work as the output field does not support a monospaced font def grades_sum(grades): total = 0 for grade in grades: total += grade return total def grades_average(grades): sum_of_grades = grades_sum(grades) average = sum_of_grades / float(len(grades)) return average def grades_variance(scores): average = grades_average(scores) ssquares = 0; for score in scores: ssquares += (average - score) ** 2 ## sum of squared difference of each score from the average score return ssquares / float(len(scores)) ## variance def grades_std_deviation(variance): return variance ** 0.5 ## square root ## grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5] print_grades(grades) print "sum ........ : ", "{:0.3f}".format(grades_sum(grades)) ## displayed with 3 decimals print "mean ....... : ", grades_average(grades) print "variance ... : ", grades_variance(grades) print "std ........ : ", grades_std_deviation(grades_variance(grades))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question