Sunday, June 8, 2025

HP Prime and TI-84 Plus CE Python: Quartiles of a Data Set

HP Prime and TI-84 Plus CE Python: Quartiles of a Data Set



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. (see Source)


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.



Python File: quartiles.py, quartile.py


This script does not use any module, so it should work on every calculator that has Python, along with the full-computer editions of Python. Results are shown as floating point numbers, rounded to 5 decimal places.


This was code was made with the HP Prime emulator, and was tested on with an HP Prime, a TI-84 Plus CE Python edition, and a TI-83 Premium CE Python Edition calculator.


# 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]))




Examples


Data Set: [1, 2, 3, 4, 5]

Min = 1.00000

Q1 = 1.50000

Med = 3.00000

Q3 = 4.50000

Max = 5.00000



Data Set: [76, 46, 49, 46, 56, 52, 59, 130, 80]

Min = 46.00000

Q1 = 47.50000

Med = 56.00000

Q3 = 78.00000

Max = 130.00000


Date Set: [55, 35, 40, 50, 60, 50, 55]

Min = 35.00000

Q1 = 40.00000

Med = 50.00000

Q3 = 55.00000

Max = 60.00000


Note: A Way to Format Numbers


To format numbers to have a set number of decimal places, use the code

{ identifier : .5f }.


The identifier can be almost anything, I usually call it 0 for the 1st number to be formatted. The number before the “f” (floating point indicator) is the number of decimal places. For example:


2 decimal places: { identifier : .2f }

5 decimal places: { identifier : .5f }

(spaces added for readability)


The text string will need to have a .format(arg) at the end of the string.


Example code:

n = 14.758119

txt = “The rounded value is {0:.5f}”

print(txt.format(n))


returns 14.75812


Source

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



Eddie


All original content copyright, © 2011-2025. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

RPN With DM32: Interest Conversions

RPN With DM32: Interest Conversions Today’s edition of RPN will focus on the Swiss Micros DM32 and HP 32SII calculator family (32S...