- Get link
- X
- Other Apps
Write a Python Program to find the maximum, minimum, mean, median and mode from a list of numbers by using user defined functions. Attach output screenshots also.
- Get link
- X
- Other Apps
Program:
l=[]n=int(input('Enter size of list: '))
def maxfunc(l):
max=l[0]
for i in range(1,len(l)):
if(max<l[i]):
max=l[i]
return max
def minfunc(l):
min = l[0]
for i in range(1, len(l)):
if (min> l[i]):
min = l[i]
return min
def meanfunc(l):
mean = l[0]
for i in range(1, len(l)):
mean += l[i]
return (mean/ len(l))
def medianfunc(l):
l.sort()
n=len(l)
if n % 2 == 0:
median1 = l[n // 2]
median2 = l[n // 2 - 1]
median = (median1 + median2) / 2
else:
median = l[n // 2]
return median
def modefunc():
global l
l.sort()
L1 = []
i = 0
while i < len(l):
L1.append(l.count(l[i]))
i += 1
d1 = dict(zip(l, L1))
d2 = {k for (k, v) in d1.items() if v == max(L1)}
print("Mode(s) is/are :" + str(d2))
for i in range(n):
l.append(int(input()))
modefunc()
max=maxfunc(l)
min=minfunc(l)
mean=meanfunc(l)
median=medianfunc(l)
print('Max value in a list is: ',max)
print('Min value in a list is: ',min)
print('Mean value in a list is: ',mean)
print('Median value in a list is: ',median)
Program images:
when list size is 5 and input is 4 ,3 ,5 ,2 ,4
Comments
Post a Comment