Update median.md

This commit is contained in:
Prajwal
2019-10-03 16:58:16 +05:30
committed by GitHub
parent b974426bcd
commit 48fe457f6f

View File

@ -4,16 +4,16 @@ tags: math,beginner
---
Finds the median of a list of numbers.
Sort the numbers of the list using `list.sort()` and find the median, which is either the middle element of the list if the list length is odd or the average of the two middle elements if the list length is even..
Sort the numbers of the list using `list.sort()` and find the median, which is either the middle element of the list if the list length is odd or the average of the two middle elements if the list length is even.
```py
def median(list):
list.sort() # The sort function of python
list_length = len(list)
if list_length%2==0:
return (list[int(list_length/2)-1] + list[int(list_length/2)])/2
else:
return list[int(list_length/2)]
list.sort()
list_length = len(list)
if list_length%2==0:
return (list[int(list_length/2)-1] + list[int(list_length/2)])/2
else:
return list[int(list_length/2)]
```
```py
median([1,2,3]) # 2