Write a function to return the median of numbers in a list


Topic: Write a function to return the median of numbers in a list

Solution

def cal_median(num_list:list)->float:
    if num_list:
        if len(num_list)%2 != 0:
            return sorted(num_list)[int(len(num_list)/2) - 1]
        else:
            return (sorted(num_list)[int(len(num_list)/2) - 1] + sorted(num_list)[int(len(num_list)/2)])/2
    else:
        return None



List all Python Programs