Informatics Practices
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: Use an inbuilt function to sort the list.
Python List Manipulation
9 Likes
Answer
n = int(input("Enter number of elements in the list: "))
nums = []
print("Enter the elements of the list:")
for i in range(n):
n1 = int(input("Enter element: "))
nums.append(n1)
nums.sort()
n = len(nums)
if n % 2 == 0:
mid1 = nums[n // 2 - 1]
mid2 = nums[n // 2]
median = (mid1 + mid2) / 2
else:
median = nums[n // 2]
print("Median of the list is:", median)Output
Enter number of elements in the list: 6
Enter the elements of the list:
Enter element: 10
Enter element: 20
Enter element: 4
Enter element: 45
Enter element: 99
Enter element: 30
Median of the list is: 25.0
Enter number of elements in the list: 5
Enter the elements of the list:
Enter element: 3
Enter element: 7
Enter element: 9
Enter element: 5
Enter element: 11
Median of the list is: 7
Answered By
4 Likes
Related Questions
Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the other having all negative numbers from the given list. Print all three lists.
Write a program to find the largest and the second largest elements in a given list of elements.
Write a program to read a list of elements. Modify this list so that it does not contain any duplicate elements, i.e., all elements occurring multiple times in the list should appear only once.
Write a program to create a list of elements. Input an element from the user that has to be inserted in the list. Also, input the position at which it is to be inserted.