hi,
I was doing exercise 15 on practice make perfect, and I have a doubt in understanding the code. here is the link https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/median?action=resume_content_item
CODE
def median(lst):
sorted_list = sorted(lst)
if len(sorted_list) % 2 != 0:
index = len(sorted_list)//2
return sorted_list[index]
elif len(sorted_list) % 2 == 0:
index_1 = len(sorted_list)/2 - 1
index_2 = len(sorted_list)/2
mean = (sorted_list[index_1] + sorted_list[index_2])/2.0
return mean
print median([2, 4, 5, 9])
Your help will be highly appreciated.
THANK YOU