Quartiles (Python)

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (https://www.hpmuseum.org/forum/forum-10.html)
+--- Forum: HP Prime Software Library (https://www.hpmuseum.org/forum/forum-15.html)
+--- Thread: Quartiles (Python) (/thread-23551.html)



Quartiles (Python) - Eddie W. Shore - 2025-05-14

25th, 50th, and 75th Percentiles

The following code finds five points of a list of data points:

Minimum: the data point of least value
Q1: the first quartile, known as the 25th percentile. The first quartile is median of the lower half of the data.
Median: also known as the 50th percentile, this is the median, or middle point of the data.
Q3: the third quartile, known as the 75th percentile. The third quartile is the median of the upper half of the data.
Maximum: the data point of the most value

The list of data is first sorted in ascending order.   

The median is determined as follows:

If the number of data points is odd: Take the number that lies in the middle.
If the number of data points is even: Take the average of the middle two numbers.

The way quartile values are determine vary depending by country and jurisdiction. The program presented uses Method 1, which is used in the United States. 

If the number of data points is odd: The data is split in half, do not include the median.
If the number of data points is even: The data is split in half.

Wikipedia. “Quartile” Last edited on February 21, 2025. https://en.wikipedia.org/wiki/Quartile Accessed May 12, 2025.


Code:
# quartiles program
# method 1, median divides data in half
# is used (US method)

# halfway subroutine
def halfway(l):
  n=len(l)
  # get halfway point
  # Python index starts at 0
  h=int(n/2)
  # even
  if n%2==0:
    return(l[h]+l[h-1])/2
  # odd 
  else:
    return(l[h])

print("Use list brackets. [ ]")  
data=eval(input("List of Data: "))

# length of the list
nw=len(data)

# sort the list
data.sort()
print("Sorted Data: "+str(data))

# maximum and minimum
q0=min(data)
q4=max(data)

# get sublists 
h=int(nw/2)
if nw%2==0:
  l1=data[0:h]
  l3=data[h:nw]
else:
  l1=data[0:h]
  l3=data[h+1:nw]

# list check
print("1st half: "+str(l1))
print("2nd half: "+str(l3))
  
# quartiles, q2 is the median
q1=halfway(l1)
q2=halfway(data)
q3=halfway(l3)

# set up  answer strings
txt1=["min = ","Q1 = ","median = ", 
"Q2 = ","max = "]
results=[q0,q1,q2,q3,q4]
for i in range(5):
  txt2="{0:.5f}"
  print(txt1[i]+txt2.format(results[i]))