Lists in Python 3

One of the most useful types in Python are the Lists. They consists in arrays of different types of information ordered, they can be manipulated and accesed in many ways. 

The source code of the program is in GitHub.

The program is about a list of 10 floating point numbers, called list1 , which will be typed in by the user. Once the user has typed the values then we will start some calculations that are defined in the functions in the Function Declaration section of the code. 

The first one takes the value of the total, originally 0.0, and then adds every single element of the list. Thus giving us the total of the numbers inside the list1. 

The second function is to calculate the average of the elements inside the list1. For this we simply use the total that we have already calculated, and divide it by the total of elements in the list1. For this we use the function len(), which applied to a list returns the number of elements contained in that list. 

The third function is the standard deviation of the list. This one is a little bit trickier. In order to calculate this another list is created, listd, which is the difference between each of the values inside list1 and the average that we have previously calculated. Once we have calculated this we need to calculate the square of the differences, and start adding them to a total variable. Once we have the total we can calculate the average of the square differences, so we divide it by the number of elements in the list.

After all those calculations we just use the math module to calculate the square root of the total.

The main topic of this post is to see how you can use a list with a for loop to read every value. Instead of using a range(), you can use a list so the for loop goes thru every single one of its values.

If the standard deviation calculation process wasn’t very clear,  here you can find a usefull page to understand it.

This is for my of my

CC BY 4.0 Lists in Python 3 by Manuel Lepe is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.