I created the following function:
def print_basic_statistics(*numbers, output = ""):
mean = (f"The mean of {numbers} is {statistics.mean(numbers):.1f}")
median = (f"The median of {numbers} is {statistics.median(numbers):.1f}")
standard_deviation = (f"The standard deviation of {numbers} is {statistics.stdev(numbers):.1f}")
variance = (f"The variance of {numbers} is {statistics.variance(numbers):.1f}")
if output == "mean":
return mean
elif output == "median":
return median
elif output == "sd":
return standard_deviation
elif output == "var":
return variance
else:
return mean, median, standard_deviation, variance
print_basic_statistics(91, 82, 19, 13, 44)
Thus, if I don't specify "output" I want all 4 to be displayed. With the code above the result is:
Out[108]:
('The mean of (91, 82, 19, 13, 44) is 49.8.',
'The median of (91, 82, 19, 13, 44) is 44.0.',
'The standard deviation of (91, 82, 19, 13, 44) is 35.6.',
'The variance of (91, 82, 19, 13, 44) is 1267.7.')
I don't like the parantheses, apostrophes and commas.
I would like the result to look like this:
Out[108]:
The mean of (91, 82, 19, 13, 44) is 49.8.
The median of (91, 82, 19, 13, 44) is 44.0.
The standard deviation of (91, 82, 19, 13, 44) is 35.6.
The variance of (91, 82, 19, 13, 44) is 1267.7.
Is there an easy way to do that?
I realize I can just write print(f"The mean of {numbers} is {statistics.mean(numbers):.1f}")
instead of return mean
, etc. But then I have to write each 'print' command twice. Once at if output == "mean"
and once at else:
.