Averages/Arithmetic mean: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: Gave Python 2.5 compatable version)
Line 300: Line 300:


=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|2.5}}
avg = lambda x: sum(x or [0])/float(len(x or [0]))
<python>def average(x):
print avg([3,1,4,1,5,9])
return sum(x)/float(len(x)) if x else 0
print average([3,1,4,1,5,9])</python>


Output:
Output:
3.83333333333333
<python>3.83333333333333</python>


def avg(data):
return sum(data)/float(len(data)) if len(data)!=0 else 0
print avg([3,1,4,1,5,9])

Output:
3.83333333333333


{{works with|Python|2.4}}
{{works with|Python|2.4}}
def avg(data):
<python>def avg(data):
if len(data)==0:
if len(data)==0:
return 0
return 0
else:
else:
return sum(data)/float(len(data))
return sum(data)/float(len(data))
print avg([3,1,4,1,5,9])
print avg([3,1,4,1,5,9])</python>


Output:
Output:
3.83333333333333
<python>3.83333333333333</python>


=={{header|Ruby}}==
=={{header|Ruby}}==