function for average in python code example
Example 1: average python
import numpy as np
values=[1,10,100]
print(np.mean(values))
values=[1,10,100,np.nan]
print(np.nanmean(values))
Example 2: python average
import statistics as st
my_list = [9, 3, 1, 5, 88, 22, 99]
print(st.mean(my_list))
Example 3: python average function program
import math
import os
import random
import re
import sys
def avg(*n):
sumOfNumber = 0
for t in n:
sumOfNumber = sumOfNumber + t
avge = sumOfNumber / len(n)
return avge
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nums = list(map(int, input().split()))
res = avg(*nums)
fptr.write('%.2f' % res + '\n')
fptr.close()
Example 4: average python
def cal_average(num):
sum_num = 0
for t in num:
sum_num = sum_num + t
avg = sum_num / len(num)
return avg
print("The average is", cal_average([18,25,3,41,5]))