Saturday, June 29, 2019

PYTHON3-AVG-EXCEPT-FIRST-AND-LAST-DATA

oyj@oyj-X555QG:~/pycode$ cat avg_middle.py
#!/usr/bin/env python3
from statistics import mean

#When you want to get avg value from list except first and last value.
#More specific example for python3 cookbook.3rd edition

def drop_first_last_avg(grades):
     first,*middle,last = grades
     return mean(middle)


data=[10,80,80,80,80,100]
print(drop_first_last_avg(data))

oyj@oyj-X555QG:~/pycode$ python3 avg_middle.py
80
oyj@oyj-X555QG:~/pycode$ chmod 700 avg_middle.py
oyj@oyj-X555QG:~/pycode$ ./avg_middle.py
80

No comments:

Post a Comment