In this short guide, I’ll show you 4 methods to calculate the mean in Python.

By the end of this guide, you’ll be able to get the mean using this simple program:

## 4 Methods to Calculate the Mean in Python

In the following section, you’ll see 4 methods to calculate the mean in Python. For each of the methods to be reviewed, the goal is to derive the mean, given the values below:

**8, 20, 12, 15, 4**

### Method 1: Simple Average Calculation

To start, you can use this simple average calculations to derive the mean:

sumValues = 8 + 20 + 12 + 15 + 4 n = 5 mean = sumValues/n print ('The Mean is: ' + str(mean))

Where:

*sumValues*represents the sum of all the values in the dataset*n*reflects the number of items in the dataset. In this case, there are 5 items*mean*= sumValues/n is the actual calculation to get the mean

When you run the code in Python, you’ll get the mean of **11.8**:

### Method 2: Using a List to get the Mean

Alternatively, you can use a *list* with the values to be averaged (here, you’ll noticed that the values are placed in a list, where each value is separated by a comma):

sum = 0 values = [8,20,12,15,4] n = len(values) for i in values: sum = sum + i mean = sum/n print ('The Mean is: ' + str(mean))

When you run the code, you’ll get the same mean of 11.8:

### Method 3: Using Pandas to get the Mean in Python

You can also derive the mean using pandas.

In our example, you’ll need to apply the syntax below to get the mean:

from pandas import DataFrame data = {'values': [8,20,12,15,4]} df = DataFrame(data) mean = df['values'].mean() print ('The Mean is: ' + str(mean))

As before, you’ll get the mean of 11.8:

### Method 4: Using a Tkinter GUI

For the final method, you can use a tkinter graphical user interface (GUI) to get the mean:

import tkinter as tk root= tk.Tk() canvas1 = tk.Canvas(root, width = 500, height = 400) canvas1.pack() label0 = tk.Label(root, text='Calculate Mean') label0.config(font=('helvetica', 14)) canvas1.create_window(250, 40, window=label0) entry1 = tk.Entry (root, width = 30) canvas1.create_window(350, 140, window=entry1) label1 = tk.Label(root, text='Values (Separated by Comma):') label1.config(font=('helvetica', 10)) canvas1.create_window(160, 140, window=label1) def calcMean (): values = entry1.get().split(',') n = len(values) sum = 0 for i in values: sum = sum + float(i) npv = sum/n label2 = tk.Label(root, text= npv,font=('helvetica', 10, 'bold'),bg='white') canvas1.create_window(250, 300, window=label2) button1 = tk.Button(text='Calculate Mean', command=calcMean, bg='green', fg='white', font=('helvetica', 9, 'bold'), width = 25) canvas1.create_window(250, 250, window=button1) root.mainloop()

Run the code in Python, and you’ll see this display:

Copy/type the values (**8, 20, 12, 15, 4**) in the input box. Each value should be separated by a comma. Once you’re done, click on the green button to calculate the mean:

You’ll then see the mean of 11.8: