Skip to main content
edited title
Link
Mathieu Guindon
  • 75.1k
  • 18
  • 191
  • 463

Taking some Python code and making it more functional Calculating average grades

Source Link
Michael A
  • 1.7k
  • 3
  • 17
  • 35

Taking some Python code and making it more functional

I'm a C# developer looking to learn more about Python and how to think in a more functional programming manner.

I have the following which takes a series of grades and returns the average:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(scores):
    total = 0
    for score in scores:
        total += score
    return total
    
def grades_average(grades):
    return grades_sum(grades) / float(len(grades))
    
print(grades_average(grades))

What are some better functional programming / pythonic ways of approaching this problem?