Python program to find median of a list
In this python program guide, we will learn to write a:
-
python program to find median of a list
Python program to find median of a list
Given below is a 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 = 0
total = 0
for i in numbers:
total += i
count += 1
if count % 2 == 0:
median = (numbers[count//2-1] + numbers[count//2])/2
else:
median = numbers[count//2]
print("The median is:",median)
The output of the python program to find median is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter numbers: 1 3 5 9 11
The median is: 5
Few important points about this 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. The time complexity of the inbuilt sort method is O(nlogn).
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 & Blockchain Enthusiast
Page Views :
Published Date :
Jan 26,2021