w3resource

Python: Calculate the median from a list of numbers

Python Basic - 1: Exercise-82 with Solution

Write a Python program to calculate the median from a list of numbers.

From Investopedia:
What is the Median?
The median is the middle number in a sorted, ascending or descending, list of numbers and can be more descriptive of that data set than the average.

    KEY TAKEAWAYS
  • The median is the middle number in a sorted, ascending or descending, list of numbers and can be more descriptive of that data set than the average.
  • The median is sometimes used as opposed to the mean when there are outliers in the sequence that might skew the average of the values.
  • If there is an odd amount of numbers, the median value is the number that is in the middle, with the same amount of numbers below and above.
  • If there is an even amount of numbers in the list, the middle pair must be determined, added together, and divided by two to find the median value.

Median Example:

To find the median value in a list with an odd amount of numbers, one would find the number that is in the middle with an equal amount of numbers on either side of the median. To find the median, first arrange the numbers in order, usually from lowest to highest.
For example, in a data set of {3, 13, 2, 34, 11, 26, 47}, the sorted order becomes {2, 3, 11, 13, 26, 34, 47}. The median is the number in the middle {2, 3, 11, 13, 26, 34, 47}, which in this instance is 13 since there are three numbers on either side.
To find the median value in a list with an even amount of numbers, one must determine the middle pair, add them, and divide by two. Again, arrange the numbers in order from lowest to highest.
For example, in a data set of {3, 13, 2, 34, 11, 17, 27, 47}, the sorted order becomes {2, 3, 11, 13, 17, 27, 34, 47}. The median is the average of the two numbers in the middle {2, 3, 11, 13, 17, 26 34, 47}, which in this case is fifteen {(13 + 17) ÷ 2 = 15}.

Sample Solution:

Python Code:

# Define a function to calculate the median of a list of numbers
def cal_median(nums):
    # Sort the list of numbers in ascending order
    nums.sort()
    
    # Get the length of the list
    n = len(nums)
    
    # Calculate the middle index
    m = n // 2
    
    # Check if the length is even or odd
    if n % 2 == 0:
        # If even, return the average of the middle two numbers
        return (nums[m - 1] + nums[m]) / 2
    else:
        # If odd, return the middle number
        return nums[m]

# Test cases
print(cal_median([1,2,3,4,5]))
print(cal_median([1,2,3,4,5,6]))
print(cal_median([6,1,2,4,5,3]))
print(cal_median([1.0,2.11,3.3,4.2,5.22,6.55]))
print(cal_median([1.0,2.11,3.3,4.2,5.22]))
print(cal_median([2.0,12.11,22.3,24.12,55.22]))

Sample Output:

3
3.5
3.5
3.75
3.3
22.3

Explanation:

Here is a breakdown of the above Python code:

  • The function "cal_median()" takes a list of numbers as input.
  • The list is sorted in ascending order using the "sort()" method.
  • The length of the list ('n') is calculated, and the middle index ('m') is determined.
  • If the length is even, the average of the two middle numbers is returned; otherwise, the middle number is returned.
  • Test cases are provided to demonstrate the function's functionality.

Visual Presentation:

Python: Calculate the median from a list of numbers.
Python: Calculate the median from a list of numbers.
Python: Calculate the median from a list of numbers.

Flowchart:

Flowchart: Python - Calculate the median from a list of numbers.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to randomly generate a list with 10 even numbers between 1 and 100 inclusive.
Next: Write a Python program to test whether a given number is symmetrical or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.