Doubt in finding the median of the list


#1

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


#2

what doubts? If we understand what doubts you are having, its much easier to help you.

You could insert a lot of print statements in the code to see what the code is doing? Essentially tearing the code apart and see what everything does, although a time intensive process, its also a good learning experience.