Python program to find median of a list

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()))

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
    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
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).


median of numbers in python


Basic Python Programs

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

About the Author
Devjeet Roy
Full Stack Web Developer & Data Science Enthusiast
Page Views :    Published Date : Jan 26,2021  
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!