Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dennisnderitu254/1585bf05c57f452fa031f5f06a3534d2 to your computer and use it in GitHub Desktop.

Select an option

Save dennisnderitu254/1585bf05c57f452fa031f5f06a3534d2 to your computer and use it in GitHub Desktop.
How to calculate Interquartile range in python
# Calculating Interquartile Range in python
# Example data (ages of students)
data = [20, 19, 21, 22, 18, 23, 20, 21, 22, 19]
# Sort the data
data.sort()
# Calculate quartiles
Q1 = data[int(len(data) / 4)]
Q3 = data[int(3 * len(data) / 4)]
# Calculate IQR
IQR = Q3 - Q1
# Print result
print("Interquartile Range (IQR) calculation:")
print(f"Q1 (first quartile): {Q1}")
print(f"Q3 (third quartile): {Q3}")
print(f"IQR: {IQR}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment