Python statistics pstdev()

The statistics.pstdev function in Python's statistics module calculates the population standard deviation of a given set of numbers. Population standard deviation measures the spread of the data points in a population dataset relative to its mean.

Table of Contents

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

Introduction

The statistics.pstdev function is part of the statistics module, which provides functions for mathematical statistics of numeric data. Population standard deviation is useful when you are dealing with an entire population dataset, as opposed to a sample of the population.

statistics.pstdev Function Syntax

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

import statistics

pstdev_value = statistics.pstdev(data)

Parameters:

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

Returns:

  • The population standard deviation of the given data.

Raises:

  • StatisticsError: If data is empty.

Examples

Basic Usage

Calculate the population standard deviation of a list of numbers.

import statistics

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

Output:

Population Standard Deviation: 1.4142135623730951

Population Standard Deviation of a List of Numbers

Calculate the population standard deviation of a list of integers.

import statistics

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

Output:

Population Standard Deviation of numbers: 14.142135623730951

Population Standard Deviation of a Tuple of Numbers

Calculate the population standard deviation of a tuple of floats.

import statistics

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

Output:

Population Standard Deviation of numbers: 1.4142135623730951

Handling Different Types of Numeric Data

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

import statistics

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

Output:

Population Standard Deviation of numbers: 1.4352700094407325

Real-World Use Case

Calculating the Population Standard Deviation of Employee Salaries

Calculate the population standard deviation of employee salaries to understand the spread of salaries in a company.

import statistics

salaries = [45000, 50000, 60000, 70000, 80000, 85000, 90000, 95000, 100000]
pstdev_salaries = statistics.pstdev(salaries)
print(f"Population Standard Deviation of salaries: {pstdev_salaries}")

Output:

Population Standard Deviation of salaries: 18708.286933869706

Conclusion

The statistics.pstdev function is a simple and effective way to calculate the population standard deviation of a set of numbers in Python. It is useful for understanding the spread and variability of an entire population dataset. This function makes it easy to determine the population 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