DAY 25 of PYTHON top 100 questions : from Basic to Advanced !!

DAY 25 of PYTHON top 100 questions : from Basic to Advanced !!

Write a Python program to find the median of a given list of numbers:

numbers = [5, 2, 9, 1, 5, 6]


numbers.sort()


n = len(numbers)


if n % 2 == 0:

    middle1 = numbers[n // 2 - 1]
    middle2 = numbers[n // 2]
    median = (middle1 + middle2) / 2
else:

    median = numbers[n // 2]


print("Median:", median)

Output :

Median: 5.0

In the provided code, we begin with a list of numbers, numbers = [5, 2, 9, 1, 5, 6], which is then sorted in ascending order. Subsequently, the length of the sorted list is determined and stored in the variable n. A check is performed to ascertain whether the length is even or odd, achieved through the modulo operator (%). If the length is even, the median is computed by averaging the two central elements, designated as middle1 and middle2. In the case of an odd length, the median is established by selecting the middle element. Finally, the calculated median value is printed to the console with the label "Median."


If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).


HAPPY LEARNING !!!