In this tutorial, we will learn how to find the first quartile in a list using Python. Quartiles are statistical measures used to divide a data set into four equal parts, each representing 25% of the data.

The first quartile (Q1) is the value that separates the lowest 25% of the data from the remaining 75%. Knowing the first quartile can help you understand the data’s distribution and identify any potential outliers or unusual values.

**Note**: In this tutorial, we will stick to simple Python constructs and the built-in `statistics`

library to calculate quartiles. However, for more advanced statistical calculations, the NumPy and Pandas libraries are recommended.

### Step 1: Sort the list in ascending order

In order to calculate the first quartile, we need the data to be sorted in ascending order. We can use the built-in `sorted()`

function to achieve this:

1 2 |
data = [10, 2, 38, 23, 38, 23, 21, 56, 45, 32] sorted_data = sorted(data) |

### Step 2: Determine the position of the first quartile

The position of the first quartile (Q1) is calculated using the formula (N + 1) / 4, where N is the number of data points in the list. However, the result might not always be an integer value, so we need to account for that:

1 |
quartile_pos = (len(sorted_data) + 1) / 4 |

If `quartile_pos`

is an integer, then the first quartile value is at that position. If `quartile_pos`

is not an integer, we need to find the values at positions just before and after the `quartile_pos`

, and then calculate the average value.

### Step 3: Find the value of the first quartile

Let’s find the value of the first quartile based on the calculated position:

1 2 3 4 5 6 7 |
if quartile_pos.is_integer(): quartile_value = sorted_data[int(quartile_pos) - 1] else: lower_val = sorted_data[int(quartile_pos) - 1] upper_val = sorted_data[int(quartile_pos)] quartile_value = (lower_val + upper_val) / 2 quartile_value = round(quartile_value, 2) |

## Example Code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
sample_data = [10, 2, 38, 23, 38, 23, 21, 56, 45, 32] sorted_data = sorted(sample_data) quartile_pos = (len(sorted_data) + 1) / 4 if quartile_pos.is_integer(): quartile_value = sorted_data[int(quartile_pos) - 1] else: lower_val = sorted_data[int(quartile_pos) - 1] upper_val = sorted_data[int(quartile_pos)] quartile_value = (lower_val + upper_val) / 2 quartile_value = round(quartile_value, 2) print("First quartile value:", quartile_value) |

## Output:

First quartile value: 15.5

## Conclusion

In this tutorial, we learned how to find the first quartile of a list in Python. We discussed the three-step process of sorting the list, determining the position, and finding the quartile value. We used the built-in `sorted()`

function to sort the data and simple if-else logic to find the quartile value. The example code provided can be adapted to find any other quartile value by changing the formula used to calculate the quartile position.