Q. Write a program to read a list of n integers and find their median.

Note:  The  median  value  of  a  list  of  values  is  the  middle one when they are arranged in order. If there are two middle values then take their average.

Hint: You can use an built-in function to sort the list


Answer :-


lst = eval(input("Enter a list :-"))
lst.sort()
length = len(lst)
if length % 2 == 0 :
    med = ( lst [ length // 2 - 1 ] + lst [ (length // 2) ]) / 2
else :
    med = lst [ (length // 2) -1 ]
print("Median :-",med)

3 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. its true for even no of integer but for odd its false because you don't have to subtract one from length its automatically taking gif of number at last the statement is :

    else:lst[(length//2)]

    ReplyDelete
    Replies
    1. No, you are wrong, because I have used median formula that is :-
      For even :- ((n-1)+(n))/2

      Delete
  2. ) missing in line1 right

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post