Python Mean, Median, Mode functions without importing anything

· Updated January 2, 2026 · Mohammad-Ali Bandzar

Header Image

Mean, Median, and Mode are the three most common types of averaging used in mathematics.

Mean:

The Mean of a list of numbers is what people most commonly refer to when talking about the “average” of a set of numbers. It is found by taking the sum of all the numbers and dividing it by the number of numbers (length of the list). In the example below, we will write a function called “mean” and it will use Python’s built-in sum() function to add up all numbers in our list and Python’s built-in len() function to get the length of our list (the number of numbers).

We can now test our function using a random list like [1,2,3,4,5][1,2,3,4,5]. Since (1+2+3+4+5)=15(1+2+3+4+5) = 15 and we have 5 numbers, we divide by 5: 155=3\frac{15}{5} = 3, so our expected output will be 3.

This function will work for any length of input.

Median:

The Median of a list of numbers will be the middle number. If there is an even number of elements in the list then we will take the mean of the middle two numbers (add them up and divide by 2).

We will start off by sorting the list and checking if the list length is odd. We will do this by checking if the length of the list modulus 2 is one: len(lst) % 2 == 1 (if you do not know what modulus is, it’s taking a number on the left of the percent sign (list length in this case) and dividing it by the number on the right (2 in this case) as many times as possible and seeing what the remainder would be). If the length is odd, then we want to return the middle number, which will be the length divided by 2 rounded down to the nearest integer: len(lst) // 2 (the two slashes represent floor division, which means that we want to round down to the nearest whole number).

The function we have written so far will work as long as lst has an odd number of elements. We now want to add an else statement which will run if the list length is even, and it will return the sum of the two middle numbers divided by two. The two middle numbers will be indexed at the length of the array divided by two minus one and the length of the array divided by two. This is because lists in Python start with an index of zero; the first element of our list can be accessed with lst[0].

Note: We still use floor division in the else statement even though we will always be left with whole numbers, because regular division returns a float type number such as 1.0, 2.0, 3.0, but we need integer type numbers (whole numbers) to use as indexes for our array. I like to use floor division; alternatively, you could write int(len(lst)), but that looks more complicated in my opinion.

We can now test it out, be sure to test both even and odd amounts of numbers to ensure the function is working correctly:

Since 3 is the middle number, we know it’s working correctly.

Since we have an even amount of numbers, we want to add the middle two together (3, 4) and divide them by two: 3+42=3.5\frac{3+4}{2} = 3.5. Since that is the same as what our function is showing us, we know it is working correctly.

Mode:

The Mode of a list is the number (or numbers) in the list which occur most frequently. We will calculate it by finding the frequency of each number present in the list and then choosing the ones with the highest frequency.

We want to start off by creating a dictionary we will call “frequency” that will store the number and its frequency. The data will be of the form frequency[number] = # of occurrences. We will then loop through our list and input all the numbers into our frequency dictionary.

Note: If you do not know what setdefault does, all it does is check if the number is in our dictionary. If it is not in our dictionary, then it will add it to our dictionary and set it equal to zero. I have added a print statement so I can show you a visual representation of what the code is doing so far.

We now want to find the highest value in our dictionary, which can be done by converting all our values into a list by using Python’s built-in .values() function on our dictionary to create a list. We can then use Python’s built-in max() function to find the highest value in that list.

Note: The print statement was added to show what is happening, and the variable names are made up by me and can be anything.

If we plug in our same data set as before:

We can see that the highest frequency number(s) occur twice in our list. We now want to create a new list by looping through our dictionary and adding any key with a value of highestFrequency (in this case 2) to it. We then want to return that list.

Note: .items() is a built-in Python function that turns a dictionary into a list of tuples that represent key-value pairs.

We can now try the example from before:

We can also try an example where every number occurs exactly once:

Since every number in our list has the same frequency, every number will be the mode.