Python - Mean, Median, And Mode Calculator

preview_player
Показать описание
In this video I show you how to create a simple mean, median, and mode calculator using python
Рекомендации по теме
Комментарии
Автор

def mean(L):
total = 0
for x in L:
total += x #total = total +x
mean = total / len(L)
return mean


def median(L):
L.sort()
if len(L)%2 != 0:
median = L[int(len(L)/2)]
else:
median = L[(int(len(L)/2)) - 1] + L[int(len(L)/2)]
median = median/2
return median


def mode(L):
counter = 0
num = L[0]

for i in L:
curr_frequency = L.count(i)
if(curr_frequency > counter):
counter = curr_frequency
num = i
if len(set(L)) == len(L):
return 'there is no mode'

return num


number_list = []

while(True):
ask = input('enter a number and say "stop" to end: ')

if ask == 'stop':
break
number_list.append(int(ask))

mean = str(mean(number_list))
median = str(median(number_list))
mode = str(mode(number_list))

print('mean: '+ mean + '\n' + 'median: ' + median + '\n' + 'mode: ' + mode)

wrttech
Автор

I just learned more from your video than I did from my whole textbook section. Thank you.

aleksanderjones
Автор

Thanks i have been searching for a mean calculator in python with a good explanation

DaniyalNasir
Автор

Thank you Wrt, this video is really helpful for me. I just get one question about the mode, if the list is [1, 1, 2, 2, 3], the answer might be [1, 2], I saw you put " if len(set(L)) == len(L):
return 'there is no mode', but I really want to know how to print the mode if the list is [1, 1, 2, 2, 3]. Thank you so much!

penggao
Автор

thnk you so much it really help me alot but i have one question how about having a class ?
how do you do it?

johnjvcabardo
Автор

Bro i need to create this in gui but i am getting very confused what should ido

prathmeshgulhane
Автор

Hey do you have a flowgorithm of this?

chinneybingka
Автор

Hi wrt. could you write comments on every step of your code please, so i can understand each step of the code as i read it (code) step by step ? i would really appreciate it. I'm new to coding.

CollinsEkogobe
Автор

hey i tried your code but it shows error
ValueError: invalid literal for int() with base 10: 'stop') help me :-)

narendramishra
Автор

What is the code when the diff elements in list having same count?
List - 2 6 3 1 8 12 2 9 10 3 4
Here 2 and 3 are mentioned two time
So output shld be mode = 2 3

sairam-nnjv
Автор

If we have more no of repetitions in a list. How, would be run a program. In your video, only one no is repeated. So, i.e., 2 has been executed. In the same way, could you show the program of repeating more nos in the mode.

srimanikanta
Автор

How would I do this without using the math and statistics modules? And how would I do this when I include def main(): I’ll show the question

This is the question I have for my homework:
Create a set of functions that compute the mean, median, and mode of a set of numbers. Section 5.4 - Example: Using a List to Find the Median of a Set of Numbers provides a simple algorithm for calculating the median for a set of numbers. Each function should expect a list of numbers as an argument and return a single number. Each function should return 0 if the list is empty. Include a main function that tests the three statistical functions with a given list. Do not use the statistics or math modules to create the functions. The functions must be created by you.

apheniesinegal