What are lists?
Remember that a list is created with square brackets [ ] and the elements have to be within them.
The elements in a list don’t have to be of the same type (you can have numbers, letters, strings).
In this example, we will create a list containing only numbers.
Create a list
myList = [1,2,3,5,8,2,5.2] i = 0 while i < len(myList): print myList[i] i = i + 1
What does this do?
This script will create a list (list1) containing the values 1,2,3,5,8,2,5.2
The while loop will print each element in the list.
Each element in the list1 is reached by the index (the letter in the square bracket).
The len function is used to get the length of the list.
We then increase the variable i with 1 for every time the while loop runs.
Output >> 1 2 3 5 8 2 5.2
The next example will count the average value of the elements in the list.
list1 = [1,2,3,5,8,2,5.2] total = 0 i = 0 while i < len(list1): total = total + list1[i] i = i + 1 average = total / len(list1) print average
#Output >> 3.74285714286