Python project 2- To find Mean, Median, and Mode ||For Absolute Beginner

python project
python projects

This is the second project for absolute beginners. As in the earlier project, we had learnt how to take input from the user and use for loop. And now, on moving forward we will learn some complex loop structure such as if-else and elif.

Description: Here first we will take user choice in the form of input whether they want to find (Mean, Median and Mode), and based on that input we will ask a user to input values and python will do some mathematical work and will give us the output.

Piece of Advice: Before starting the project, you must know what is Mean,Median and Mode.

What you will learn?: By completing this project , you will learn-

  • How to take input from the user?
  • Based on the input provided, How to run specific code using if-else and elif.

Let’s start……

Step 1: Take Input from the user and see which if else condition satisfy the input given by user.

first_input  =input("Enter 'M' to find Mean,'Med' to find Median and 'Mo' to find Mode: ")

if first_input =='M':
	print("You selected Mean")

elif first_input =='Med':
	print("You selected Median")

elif first_input == 'Mo':
	print("You selecetd Mode")

else:
	print("Enter choice correctly again .")

Step 2: Let’s work on Mean apart. As we know, the mean is equal to the sum of all numbers divided by the total number of values.

if first_input =='M':
	mean_input = input("Enter the values  separated by commma ',' for which you want to find Mean : ")
	mean_list = mean_input.split(',')
	sum_of_all_numbers= 0
	for i in mean_list:
		sum_of_all_numbers=int(sum_of_all_numbers)+int(i)
	#Now we Apply Mean Formula which  is  sum of all values/total no of values
	mean = sum_of_all_numbers/len(mean_list)
	print('Mean is :',mean)

Step 3: Now, let’s move to third part which is to find median. As you know, median is defined as middle value of the list of values. Here, we need to first sort the list ,then we need to check of number of values are odd or even.

median_input = input("Enter the values separated by comman ',' for which you want to find Median : ")
	median_list = median_input.split(',')
	number_of_values = len(median_list)
	median_list.sort()

	if number_of_values % 2 ==0:
		median1 = int(median_list[number_of_values//2])
		median2 = int(median_list[number_of_values//2 - 1] )
		median = (median1 + median2)/2
		
		print(median)
	else:
		median = median_list[number_of_values//2]
		print(median)

Step 3: Next, we need to write code to find the mode of the values given by the user. The mode is the number that is repeated more often than any other number.

elif first_input == 'Mo':
	mode_input = input("Enter the values separated by comman ',' for which you want to find Median : ")
	mode_list = mode_input.split(',')
	mode_count = Counter(mode_list)
	mode =max(mode_count,key=mode_count.get)
	print('Mode of given values : ',mode)

Step 4: Now last step is to write , else condition, In case, if user has done some typo mistake or written something else ,it should give an error message.

else:
	print("Enter choice correctly again .")

Congratulations, You had completed your 2nd project.

from collections import Counter 
first_input  =input("Enter 'M' to find Mean,'Med' to find Median and 'Mo' to find Mode: ")

if first_input =='M':
	mean_input = input("Enter the values  separated by commma ',' for which you want to find Mean : ")
	mean_list = mean_input.split(',')
	sum_of_all_numbers= 0
	for i in mean_list:
		sum_of_all_numbers=int(sum_of_all_numbers)+int(i)
	#Now we Apply Mean Formula which  is  sum of all values/total no of values
	mean = sum_of_all_numbers/len(mean_list)
	print('Mean is :',mean)


elif first_input =='Med':
	median_input = input("Enter the values separated by comman ',' for which you want to find Median : ")
	median_list = median_input.split(',')
	number_of_values = len(median_list)
	median_list.sort()

	if number_of_values % 2 ==0:
		median1 = int(median_list[number_of_values//2])
		median2 = int(median_list[number_of_values//2 - 1] )
		median = (median1 + median2)/2
		
		print(median)
	else:
		median = median_list[number_of_values//2]
		print(median)


elif first_input == 'Mo':
	mode_input = input("Enter the values separated by comman ',' for which you want to find Median : ")
	mode_list = mode_input.split(',')
	mode_count = Counter(mode_list)
	mode =max(mode_count,key=mode_count.get)
	print('Mode of given values : ',mode)
else:
	print("Enter choice correctly again .")
mean using python
mean using python
median using python
median using python
mode using python
mode using python

Python had some statistical inbuilt python function. Let me show you how to do that.

Mean :

import statistics
mean_input = input("Enter the values  separated by commma ',' for which you want to find Mean : ")
mean_list = mean_input.split(',')
print(statistics.mean(mean_list))

Median

import statistics
median_input = input("Enter the values  separated by commma ',' for which you want to find Median : ")
median_list = median_input.split(',')
print(statistics.median(median_list))

Mode

import statistics
mode_input = input("Enter the values  separated by commma ',' for which you want to find Mode : ")
mode_list = mode_input.split(',')
print(statistics.mode(mode_list))

Be the first to comment

Leave a Reply

Your email address will not be published.


*