Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create mean_median.py #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions mean_median.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from collections import Counter # Import Counter from the collections module for finding the mode.

def calculate_mean_median_mode(numbers):
# Calculate the mean (average) of the numbers.
mean = sum(numbers) / len(numbers)

# Calculate the median of the numbers.
sorted_numbers = sorted(numbers)
n = len(sorted_numbers)
if n % 2 == 1:
median = sorted_numbers[n // 2]
else:
mid1 = sorted_numbers[(n - 1) // 2]
mid2 = sorted_numbers[n // 2]
median = (mid1 + mid2) / 2

# Calculate the mode (most common value) of the numbers.
count = Counter(numbers)
mode_count = max(count.values())
mode = [number for number, freq in count.items() if freq == mode_count]

return mean, median, mode

# Example usage:
numbers = [1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 7, 7, 8]
mean, median, mode = calculate_mean_median_mode(numbers)
print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)