Program to get the median of given numbers.

This Python basic program will get the median of given numbers. 

Steps to solve the program

  1. Take a list as input.
  2. Sort the given list using sort().
  3. Get the length of the list.
  4. If the length of the list is odd, then use the formula
    (n+1)/2 to find the median of the given list.
  5. If the length of the list is odd, then use the formula.
    Use the formula (n+1)/2 to find the median of the given list, where n is the length of the list.
  6. If the length of the list is even, then use the formula.
    Use the formula ((n/2 – 1) + n/2)/2 to find the median of the given list, where n is the length of the list
  7. Print the output.


Program:

# example list of values
values = [10, 20, 30, 40, 50]

# sort the list
values.sort()

# get length of the list
n = len(values)

if n % 2 == 1:
    median_value = values[n // 2]
else:
    median_value = (values[n // 2 - 1] + values[n // 2]) / 2

print(f"The median is: {median_value}")

Output:

The median is: 30


Leave a Comment