Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python statistics.mode() Method

Python statistics.mode() Method

Python statistics Module Python statistics Module

Python statistics.mode() Used to compute the mode of a given data set.

Syntax

The syntax of the statistics.mode() method is as follows:


statistics.mode(data)

Parameter Description:

Return Value

The function computes and returns the mode of the data set as the result.

The mode is the value or set of values that occurs most frequently in a data set.

Example

The following example demonstrates the mode() function:

Example

import

statistics

data

=

[

1

,

2

,

3

,

3

,

4

,

4

,

4

,

5

]

mode

=

statistics.

mode

(

data

)

print

(

mode

)

Output result:


4

In the above example, the given data set is [1, 2, 3, 3, 4, 4, 4, 5], and the mode() method computes a mode of 4.

Note that if a data set has multiple modes with the same frequency, the mode() method returns the smallest mode.

The mode() method is often used to analyze central tendency and common values in a data set. In some cases a data set may have no mode or multiple modes; in that situation you can catch the statistics.StatisticsError exception to handle this case.

Python statistics Module Python statistics Module