Python statistics.median() method is **“used to calculate the median (middle value) of the given data set.”**

**Syntax**

```
median([data-set])
```

**Parameters**

**[data]:** It is a list or tuple or an iterable with a set of numeric values.

**Return value**

It returns the median (middle value) of the iterable containing the data.

**Example 1: How to Use statistics.median() method**

```
import statistics
listA = [19, 46, 21, 18, 30]
print(statistics.median(listA))
```

**Output**

**Example 2: StatisticsError**

```
from statistics import median
# creating an empty data-set
empty = []
# will raise StatisticsError
print(median(empty))
```

**Output**

`statistics.StatisticsError: no median for empty data`

**Example 3**

```
from statistics import median
from fractions import Fraction as fr
data1 = (12, 21, 19, 21, 46)
data2 = (1.9, 2.1, 3.9, 8.9)
data3 = (fr(11, 22), fr(44, 12),
fr(21, 19), fr(22, 32))
data4 = (-5, -1, -12, -19, -3)
data5 = (-1, -2, -3, -4, 4, 3, 2, 1)
print("Median of data 1 is % s" % (median(data1)))
print("Median of data 2 is % s" % (median(data2)))
print("Median of data 3 is % s" % (median(data3)))
print("Median of data 4 is % s" % (median(data4)))
print("Median of data 5 is % s" % (median(data5)))
```

**Output**

`Median of data 1 is `**21**
Median of data 2 is **3.0**
Median of data 3 is **545/608**
Median of data 4 is **-5**
Median of data 5 is **0.0**

That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.