Python For Loops

Lists

Looping through lists is one of the key skills you need to master in python.

You can iterate through elements of a list by going through them sequentially.

list1 = [0,1,2,3,4,5,6,7,8,9]

for value in list1: 
    print(value)

#Note the variable value could have any name.
#This code, although ugly will produce the same result.

for asdf in list1: 
    print(asdf)

If you want to have access to the value of a list and its index simultaneously.

for index, value in enumerate(list1):
    print(index, value)

#or using the native range() and len() functions

for i in range(len(list1)):
    print(i, list1[i])

Now, a couple of useful things to know.
More often than not, you are going to use the .append() method to add elements to a list.

#This code will add the squares of all numbers 
#present in list1 before the iteration

list1 = [0,1,2,3,4,5,6,7,8,9]
list2 = []

for value in list1: 
    list2.append(value**2)