This Python basic program will get the median of given numbers.
Steps to solve the program
- Take a list as input.
- Sort the given list using sort().
- Get the length of the list.
- If the length of the list is odd, then use the formula
(n+1)/2 to find the median of the given list. - 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. - 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 - 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