Python statistics.stdev() Method with Examples

statistics.stdev() Method in Python:

Statistics.stdev() computes the standard deviation from a sample of data.

The standard deviation measures how evenly distributed the numbers are.

A high standard deviation indicates that the data is dispersed, whereas a low standard deviation indicates that the data is tightly clustered around the mean.

Tip: Unlike variance, the standard deviation is expressed in the same units as the data.

The standard deviation is equal to the square root of the sample variance.

Look at the statistics.pstdev() method to calculate the standard deviation of an entire population.

Syntax:

statistics.stdev(data, xbar)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

xbar: This is Optional. The arithmetic mean of the given data. If omitted (or set to None), the mean is calculated automatically.

Note: It should be noted that if the data has fewer than two values, StatisticsError is returned.

Return Value:

Returns a float value representing the given data’s standard deviation.

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The standard deviation of the given list items [10, 20, 40, 15, 30] =  12.041594578792296

Example2:

Input:

Given list = [-2, 5, 8, 9, 0, -1]

Output:

The standard deviation of the given list items [-2, 5, 8, 9, 0, -1] = 4.792355023020171

statistics.stdev() 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.stdev() method that computes the standard deviation from a sample of data(here given list).
  • Store it in another variable.
  • Print the standard deviation 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.stdev() method that
# computes the standard deviation from a sample of data(here given list).
# Store it in another variable.
rslt = statistics.stdev(gvn_lst)
# Print the standard deviation of the given list items.
print("The standard deviation of the given list items", gvn_lst, "= ", rslt)

Output:

The standard deviation of the given list items [10, 20, 40, 15, 30] =  12.041594578792296

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.stdev() method that computes the standard deviation from a sample of data(here given list).
  • Store it in another variable.
  • Print the standard deviation 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.stdev() method that
# computes the standard deviation from a sample of data(here given list).
# Store it in another variable.
rslt = statistics.stdev(gvn_lst)
# Print the standard deviation of the given list items.
print("The standard deviation of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = -2 5 8 9 0 -1
The standard deviation of the given list items [-2, 5, 8, 9, 0, -1] = 4.792355023020171