calculate the average of highest 5 numbers in a list python code example

Example 1: find average of list python

list = [15, 18, 2, 36, 12, 78, 5, 6, 9]

# for older versions of python
average_method_one = sum(list) / len(list) 
# for python 2 convert len to a float to get float division
average_method_two = sum(list) / float(len(list))

# round answers using round() or ceil()
print(average_method_one)
print(average_method_two)

Example 2: python average function program

#!/bin/python3

import math
import os
import random
import re
import sys


# write your code here
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 3: my_mode() python

>>> from collections import Counter

>>> def my_mode(sample):
...     c = Counter(sample)
...     return [k for k, v in c.items() if v == c.most_common(1)[0][1]]
...

>>> my_mode(["male", "male", "female", "male"])
['male']

>>> my_mode(["few", "few", "many", "some", "many"])
['few', 'many']

>>> my_mode([4, 1, 2, 2, 3, 5])
[2]

>>> my_mode([4, 1, 2, 2, 3, 5, 4])
[4, 2]