Median Calculation with Python

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In statistics, the median is a measure of central tendency that represents the middle value of a dataset. It is the value separating the higher half from the lower half of a data sample. In this challenge, you will be required to write a Python function that finds the median of a list of numbers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13689{{"`Median Calculation with Python`"}} python/variables_data_types -.-> lab-13689{{"`Median Calculation with Python`"}} python/numeric_types -.-> lab-13689{{"`Median Calculation with Python`"}} python/type_conversion -.-> lab-13689{{"`Median Calculation with Python`"}} python/conditional_statements -.-> lab-13689{{"`Median Calculation with Python`"}} python/lists -.-> lab-13689{{"`Median Calculation with Python`"}} python/tuples -.-> lab-13689{{"`Median Calculation with Python`"}} python/function_definition -.-> lab-13689{{"`Median Calculation with Python`"}} python/data_collections -.-> lab-13689{{"`Median Calculation with Python`"}} python/build_in_functions -.-> lab-13689{{"`Median Calculation with Python`"}} end

Median

Write a Python function called find_median that takes a list of numbers as an argument and returns the median of the list. Your function should perform the following steps:

  1. Sort the numbers of the list using list.sort().
  2. 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.
  3. Return the median.

Your function should not use any built-in Python libraries or functions that directly solve the problem.

def median(list):
  list.sort()
  list_length = len(list)
  if list_length % 2 == 0:
    return (list[int(list_length / 2) - 1] + list[int(list_length / 2)]) / 2
  return float(list[int(list_length / 2)])
median([1, 2, 3]) ## 2.0
median([1, 2, 3, 4]) ## 2.5

Summary

In this challenge, you have learned how to find the median of a list of numbers using Python. You have written a function that sorts the list and finds the median by checking the length of the list and returning the appropriate value.

Other Python Tutorials you may like