Lists in Python

In Python we have a really easy way of iterating through a list (called arrays in other languages). A list is simply a collection of items in order. Python (unlike some other languages) doesn't verify that the items in the list are the same type. It is possible to put many different types, including complex objects, into a list alongside simple types (like number and strings). This is very dangerous and will likely lead to errors in your code in the future. It is advisable to avoid mixing types in lists.

Creating a list

A list can be created and stored in a variable, like so
mylist = [1, 2, 3]
and we can access each item individually by using an index. Note that Python (and almost every other sane language) use what are known as 'zero-based' arrays. This means that the first item in the list is at position 0, the second at position 1 and so on and so forth.
print( mylist[0] ) # outputs 1
print( mylist[1] ) # outputs 2
print( mylist[2] ) # outputs 3

Adding to a list

We can also add things to a list after it has been created using the append function.
fruits = ['apples', 'bananas', 'melons']

fruits.append( 'plums' )
fruits.append( 'grapes' )

print(fruits) # outputs ['apples','bananas','melons','plums','grapes']

Iterating through a list

Now that we have our list we can take advantage of one of the nicest parts of Python, it's ability to quickly and easily iterate through a list. We use the follow command to loop through the list in order. Each time we loop through our item is assigned to the variable name that we give it, in this case it will be fruit. (NOTE: The tab before 'print( fruit )' is important because it denotes where the looped code is)
fruits = ['apples', 'bananas', 'melons']

for fruit in fruits:
print( fruit )

# outputs ->
# apples
# bananas
# melons