Python statistics stdev()

The statistics.stdev function in Python's statistics module calculates the standard deviation of a given set of numbers. Standard deviation is a measure of the amount of variation or dispersion in a dataset. It provides insight into how spread out the values are around the mean.

Table of Contents

  1. Introduction
  2. statistics.stdev Function Syntax
  3. Examples
    • Basic Usage
    • Standard Deviation of a List of Numbers
    • Standard Deviation of a Tuple of Numbers
    • Handling Different Types of Numeric Data
  4. Real-World Use Case
  5. Conclusion

Introduction

The statistics.stdev function is part of the statistics module, which provides functions for mathematical statistics of numeric data. The standard deviation is useful for understanding the variability of a dataset.

statistics.stdev Function Syntax

Here's how you use the statistics.stdev function:

import statistics

stdev_value = statistics.stdev(data)

Parameters:

  • data: A sequence or iterable of numeric data (list, tuple, etc.).

Returns:

  • The standard deviation of the given data.

Raises:

  • StatisticsError: If data has fewer than two values.

Examples

Basic Usage

Calculate the standard deviation of a list of numbers.

import statistics

data = [1, 2, 3, 4, 5]
stdev_value = statistics.stdev(data)
print(f"Standard Deviation: {stdev_value}")

Output:

Standard Deviation: 1.5811388300841898

Standard Deviation of a List of Numbers

Calculate the standard deviation of a list of integers.

import statistics

numbers = [10, 20, 30, 40, 50]
stdev_value = statistics.stdev(numbers)
print(f"Standard Deviation of numbers: {stdev_value}")

Output:

Standard Deviation of numbers: 15.811388300841896

Standard Deviation of a Tuple of Numbers

Calculate the standard deviation of a tuple of floats.

import statistics

numbers = (1.5, 2.5, 3.5, 4.5, 5.5)
stdev_value = statistics.stdev(numbers)
print(f"Standard Deviation of numbers: {stdev_value}")

Output:

Standard Deviation of numbers: 1.5811388300841898

Handling Different Types of Numeric Data

Calculate the standard deviation of a mixed list of integers and floats.

import statistics

numbers = [1, 2.5, 3, 4.5, 5]
stdev_value = statistics.stdev(numbers)
print(f"Standard Deviation of numbers: {stdev_value}")

Output:

Standard Deviation of numbers: 1.6046806535881213

Real-World Use Case

Calculating the Variability in Test Scores

Calculate the standard deviation of test scores to understand the variability among students.

import statistics

test_scores = [88, 92, 80, 89, 95, 91, 85, 94, 90, 87]
stdev_test_scores = statistics.stdev(test_scores)
print(f"Standard Deviation of test scores: {stdev_test_scores}")

Output:

Standard Deviation of test scores: 4.433458644845529

Conclusion

The statistics.stdev function is a simple and effective way to calculate the standard deviation of a set of numbers in Python. It is useful for understanding the variability and dispersion of a dataset. This function makes it easy to determine the standard deviation, which is a common requirement in various fields such as finance, science, and engineering.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare