Averages/Arithmetic mean: Difference between revisions

→‎{{header|Python}}: Gave Python 2.5 compatable version
(→‎{{header|Python}}: Gave Python 2.5 compatable version)
Line 300:
 
=={{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(datax)/float(len(datax)) if len(data)!=0x else 0
print avgaverage([3,1,4,1,5,9])</python>
 
Output:
<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}}
<python>def avg(data):
if len(data)==0:
return 0
else:
return sum(data)/float(len(data))
print avg([3,1,4,1,5,9])</python>
 
Output:
<python>3.83333333333333</python>
 
=={{header|Ruby}}==
Anonymous user