how to calculate the average in python code example

Example 1: average value of list elements in python

# Example to find average of list
number_list = [45, 34, 10, 36, 12, 6, 80]
avg = sum(number_list)/len(number_list)
print("The average is ", round(avg,2))

Example 2: 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 3: how to calculate mean in python

import statistics

a = [1,2,3,4,5]

mean = statistics.mean(a) 
#Similar for other values such as variance, standard deviation

Example 4: how to use a function to find the average in python

avreage_cost = cost
    avg = sum(avreage)/len(avreage) 
    print("The average amout you spent is ", round(avg,2))

Example 5: calcutalte average python

# app.py

def averageOfList(num):
    sumOfNumbers = 0
    for t in num:
        sumOfNumbers = sumOfNumbers + t

    avg = sumOfNumbers / len(num)
    return avg


print("The average of List is", averageOfList([19, 21, 46, 11, 18]))

Example 6: how to find the average in python

import numpy

 avreage_1 = numpy.mean(avreage)# this finds the mean from the array "cost"
    print("words are printed here if needed",avreage_1) # this prints the mean that was found above