Articles

Program to find the mean and median of numbers present in a list in Python

Program to find the mean and median of numbers present in a list in Python


In this python programming guide, we are going to learn:

  • python program to calculate mean of a given list of numbers,
  • python program to find median of a list

Program to find the mean and median of numbers present in a list in Python

The code will help us in understanding python program to calculate mean of a given list of numbers & python program to find median of a list.

# Owner : TutorialsInhand Author : Devjeet Roy

numbers = list(map(int, input("Enter numbers: ").strip().split()))
numbers.sort()

count = len(numbers)
total = sum(numbers)

mean = total/count

if count % 2 == 0:
    median = (numbers[count//2-1] + numbers[count//2])/2
else:
    median = numbers[count//2]

print("The mean is:", round(mean,2))
print("The median is:",median)

The output of the program how to calculate mean median in python is as follows:

 

Enter numbers: 1 2 3 100
The mean is: 26.5
The median is: 2.5

Few important tips about the program:

1. Median is the number from where the rest of the numbers are equally distibuted at both the sides.

2. For odd number of insertions, median is the middle element of the sorted list. For even number of elements, median is the average of the middle two elements of the sorted list.

3. We have used sort() method, two sort the list in place.

4. The mean of average is the representation of the values in a list.

5. The sum() function returns the sum of elements present in a list and the len() function returns the number of elements present in a list.

 

how to calculate mean median in python

Mean and median of numbers present in a list in python program

This wraps up our session on program to find the average of numbers in a list in python (mean) and python program to find median of a list.


Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Data Science Enthusiast
Page Views :    Published Date : Jun 24,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!