statistics.mean() Method in Python:
The statistics. mean() method computes the mean (average) of a data set given.
mean: To calculate the mean, add all of the given values and divide by the number of values.
for example
let list = [5, 10, 15, 20]
mean= 5+10+15+20/4 = 12.5
Syntax:
statistics.mean(data)
Parameters
data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).
Note: It is to be noted that if the data is empty, it returns a StatisticsError.
Return Value:
Returns a float value that represents the mean of the given data.
Examples:
Example1:
Input:
Given list = [10, 20, 40, 15, 30]
Output:
The mean of the given list items [10, 20, 40, 15, 30] = 23
Example2:
Input:
Given list = [-1, -2, 6, 7, 4, 9]
Output:
The mean of the given list items [-1, -2, 6, 7, 4, 9] = 3.8333333333333335
statistics.mean() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import statistics module using the import keyword.
- Give the list as static input and store it in a variable.
- Pass the given list as an argument to the statistics.mean() method that computes the mean (average) of the given list items.
- Store it in another variable.
- Print the mean (average) of the given list items.
- The Exit of the Program.
Below is the implementation:
# Import statistics module using the import keyword. import statistics # Give the list as static input and store it in a variable. gvn_lst = [10, 20, 40, 15, 30] # Pass the given list as an argument to the statistics.mean() method that # computes the mean (average) of the given list items. # Store it in another variable. rslt = statistics.mean(gvn_lst) # Print the mean (average) of the given list items. print("The mean of the given list items", gvn_lst, " = ", rslt)
Output:
The mean of the given list items [10, 20, 40, 15, 30] = 23
Method #2: Using Built-in Functions (User Input)
Approach:
- Import statistics module using the import keyword.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in another variable.
- Pass the given list as an argument to the statistics.mean() method that computes the mean (average) of the given list items.
- Store it in another variable.
- Print the mean (average) of the given list items.
- The Exit of the Program.
Below is the implementation:
# Import statistics module using the import keyword. import statistics # Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Pass the given list as an argument to the statistics.mean() method that # computes the mean (average) of the given list items. # Store it in another variable. rslt = statistics.mean(gvn_lst) # Print the mean (average) of the given list items. print("The mean of the given list items", gvn_lst, " = ", rslt)
Output:
Enter some random List Elements separated by spaces = -1 -2 6 7 4 9 The mean of the given list items [-1, -2, 6, 7, 4, 9] = 3.8333333333333335