You can use the statistics module.
A cool feature is, that you can use also Fraction. Math teacher will love this.
A cool feature is, that you can use also Fraction. Math teacher will love this.
import statistics data = { 'Jeremy':73284, 'Hansel':8784.3, 'Uee':9480938.2, 'Seolhyun':984958.3, 'Ketsuno Ana':24131, 'Trump':45789 } mean = statistics.mean(data.values()) median = statistics.median(data.values()) h_mean = statistics.harmonic_mean(data.values()) print("The mean networth is:", mean) print("The median networth is:", median) print("The h_mean networth is:", h_mean)You can use formatting (>= Python 2.7):
print("The mean networth is: {:.2f}".format(mean)) print("The median networth is: {:.2f}".format(median)) print("The h_mean networth is: {:.2f}".format(h_mean))
Output:The mean networth is: 1769647.47
The median networth is: 59536.50
The h_mean networth is: 31268.64
And since Python 3.6 you could use Literal String Interpolation, better known as format strings. The syntax is equal, the only difference is, that the interpolation is accessing local variables.print(f"The mean networth is: {mean:.2f}") print(f"The median networth is: {median:.2f}") print(f"The h_mean networth is: {h_mean:.2f}")
Output:The mean networth is: 1769647.47
The median networth is: 59536.50
The h_mean networth is: 31268.64
By the way, other functions you can use on the ValuesView
, which is an Iterable
.data = { 'Jeremy':73284, 'Hansel':8784.3, 'Uee':9480938.2, 'Seolhyun':984958.3, 'Ketsuno Ana':24131, 'Trump':45789 } dict_values = data.values() # the dict_values is the view to the values of the dictionary # it's accessing the original dict # if you update a value or add a key with a value # the view is updated print("Before Update") min_val, max_val = min(dict_values), max(dict_values) print(f"networth min: {min_val:.2f}\nnetworth max: {max_val:.2f}") # calculate and print values, before updating mean = statistics.mean(dict_values) median = statistics.median(dict_values) h_mean = statistics.harmonic_mean(dict_values) print(f"The mean networth is: {mean:.2f}") print(f"The median networth is: {median:.2f}") print(f"The h_mean networth is: {h_mean:.2f}") # updating the dict data["Angela Merkel"] = -666 print("After Update") min_val, max_val = min(dict_values), max(dict_values) print(f"networth min: {min_val:.2f}\nnetworth max: {max_val:.2f}") mean = statistics.mean(dict_values) median = statistics.median(dict_values) # h_mean = statistics.harmonic_mean(dict_values) # StatisticsError: harmonic mean does not support negative values print(f"The mean networth is: {mean:.2f}") print(f"The median networth is: {median:.2f}")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!