Write a python program to find the median of three values
In this post, you will learn how to find the median of three values using the Python programming language. There are many ways to find the middle number of three numbers. Here, we have explained most of them.
Median is a measurement of the centre of a numerical data set. Median is the middle value of the given list of data when arranged in an order. The arrangement of data can be done either in ascending or descending order. For example, 3,5,8,11,12 is the given set of data. Then, the median value is 8. For an odd number of data set, the median value is the middlemost data, and for an even number of data set, the median value is the average of the two middle values.
Median Formula
It is easy to find the median if we have an odd number of observations. Suppose we have 'n' even observations, then the median formula of a given set of numbers is calculated as-
Median = [(n + 1)/2]th term
Python find the median number of three numbers using if-else statement
In the given Python program, we have used the if-else statement to find the median number of three numbers.
# Python program to find median
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if num1 > num2:
if num1 < num3:
median = num1
elif num2 > num3:
median = num2
else:
median = num3
else:
if num1 > num3:
median = num1
elif num2 < num3:
median = num2
else:
median = num3
print("The median number is", median)
Output of the above code:
Enter first number: 64
Enter second number: 23
Enter third number: 53
The median number is 53.0
Python find the median number of three numbers using if statements with and operator
In the given Python program, we have asked the user to enter three numbers and used the if statements with the AND operator to find the median value.
# Python program to find median
# using if statements with and operator
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
# checking a is a middle number or not
if(b>a and a>c or c>a and a>b):
print(a, " is median")
# checking b is a middle number or not
if(a>b and b>c or c>b and b>a):
print(b, "is median")
# checking c is a middle number or not
if(a>c and c>b or b>c and c>a):
print(c, "is median")
Output of the above code:
Enter first number: 54
Enter second number: 52
Enter third number: 62
54 is median
Python find the median number of three numbers using median() method
Python has a built-in module statistics that we can use to calculate mathematical statistics of numeric data. This module provides the median() method to calculate the median of the given data set.
# Python program to find median
# importing statistics module
import statistics
# initializing unsorted list
data1 = [22, -43, 75]
# Printing median of the
# data-set using median() method
print("Median of data is : % s "
% (statistics.median(data1)))
Output of the above code:
Median of data is : 22
Related Articles
Quicksort program in PythonTower of Hanoi Python
Python program to find roots of quadratic equation
Find Power of a Number in Python
Convert Python list to numpy array
Convert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary