Aim: Write a python program to compute Central Tendency Measures: Mean, Median, Mode Measure of Dispersion: Variance, Standard Deviation

Mean:

# Python program to print the mean of elements

# List of elements to calculate mean
n_num = [1, 2, 3, 4, 5]

# Calculate the number of elements
n = len(n_num)

# Calculate the sum of elements
get_sum = sum(n_num)

# Calculate the mean
mean = get_sum / n

# Print the result
print("Mean / Average is: " + str(mean))
        
Output:

Mean / Average is: 3.0

Median:

# Python program to print the median of elements

# List of elements to calculate the median
n_num = [1, 2, 3, 4, 5]

# Number of elements in the list
n = len(n_num)

# Sort the list
n_num.sort()

# Calculate median
if n % 2 == 0:
    median1 = n_num[n // 2]
    median2 = n_num[n // 2 - 1]
    median = (median1 + median2) / 2
else:
    median = n_num[n // 2]

print("Median is:", median)
        
Output:

Median is: 3

Mode:

# Python program to print the mode of elements
from collections import Counter

# List of elements to calculate mode
n_num = [1, 2, 3, 4, 5, 5]

# Count the frequency of each element
data = Counter(n_num)

# Get the mode(s)
get_mode = dict(data)
mode = [k for k, v in get_mode.items() if v == max(list(data.values()))]

# Check if there is a mode
if len(mode) == len(n_num):
    get_mode = "No mode found"
else:
    get_mode = "Mode is/are: " + ', '.join(map(str, mode))

# Print the result
print(get_mode)
        
Output:

Mode is/are: 5

Variance:

# Python program to get the variance of a list

# Importing the NumPy module
import numpy as np

# Taking a list of elements
numbers = [2, 4, 4, 4, 5, 5, 7, 9]

# Calculating variance using var()
variance = np.var(numbers)

# Printing the variance
print("Variance of the list is:", variance)
        
Output:

Variance of the list is: 4.0

Standard Deviation:

# Python program to get the standard deviation of a list

# Importing the NumPy module
import numpy as np

# Taking a list of elements
numbers = [2, 4, 4, 4, 5, 5, 7, 9]

# Calculating standard deviation using std()
std_deviation = np.std(numbers)

# Printing the standard deviation
print("Standard Deviation of the list is:", std_deviation)
        
Output:

Standard Deviation of the list is: 2.0

Sample  Viva Questions

1. What are Central Tendency Measures: Mean, Median, Mode

  • Central Tendency Measures are statistical tools used to summarize a set of data points. These measures include:
    • Mean: The average of all numbers in a dataset.
    • Median: The middle value when the data points are ordered in ascending or descending order.
    • Mode: The number that appears most frequently in a dataset.

2. Define Mean, Mode, and Median

  • Mean: The mean is the average of all numbers in a dataset. It is calculated by summing all the values and dividing by the total number of values.
  • Median: The median is the middle number in a set of ordered numbers. If the dataset has an odd number of values, the median is the middle value. If even, it is the average of the two middle values.
  • Mode: The mode is the number that occurs most frequently in a dataset. If no number repeats, the dataset is said to have no mode. If multiple numbers repeat, it is multimodal.

3. Define Variance, Standard Deviation

  • Variance: Variance is a measure of how much the numbers in a dataset differ from the mean. It is calculated by finding the average of the squared differences from the mean.
    • Formula for variance:

      Variance = (1/n) * Σ (xi – μ)2

  • Standard Deviation: Standard deviation is the square root of the variance and provides an indication of how spread out the data is from the mean.

4. What is the syntax for input and output in Python?

Input: In Python, we use the input() function to take input from the user. It always returns the input as a string by default python

5. Which python version your using?

To check your Python version, run python ‐‐version in your command line (Windows), shell (Mac), or terminal (Linux/Ubuntu).

 
;