Answer to Question #317638 in Python for venky

Question #317638

write a program find mean,median and mode by using functions in python



1
Expert's answer
2022-03-24T13:22:14-0400
def mean(numbers):
    return round(sum(numbers) / len(numbers), 2)


def median(numbers):
    index = len(numbers) // 2
    if len(numbers) % 2:
        return sorted(numbers)[index]
    else:
        return sum(sorted(numbers)[index - 1:index + 1]) / 2


def mode(numbers):
    counts = dict.fromkeys(set(numbers))
    for key in counts:
        counts[key] = numbers.count(key)
    mode_value = sorted(counts.items(), key=lambda x: x[1], reverse=True)[0]
    if mode_value[1] != 1:
        return mode_value[0]
    else:
        return "No mode!"



def main(numbers):
    print(f"Mean - {mean(numbers)}")
    print(f"Median - {median(numbers)}")
    print(f"Mode - {mode(numbers)}")



if __name__ == "__main__":
    numbers = [3, 5, 1, 4, 5, 20, 6]
    main(numbers)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS