for loop

loops are great. we can do things over and over until something breaks.

not that kind of loop.

this one looks a bit more reasonable.

for loop.... for list

There are many kinds of for-loop you can write. We focus on the simplest for loop with list here.

wait

make sure you read everything about list from list1.py to list5.py

list4.py is too much to type

        # Create two lists x and y containing some numbers
        x = [3, 2, 1, 0]
        y = [7, 6, 5, 4]

        # Now ask yourself how to read the value of x and y?
        # print them out one at a time!

        print 'x[0] is', x[0]
        print 'x[1] is', x[1]
        print 'x[2] is', x[2]
        print 'x[3] is', x[3]

        print 'y[0] is', y[0]
        print 'y[1] is', y[1]
        print 'y[2] is', y[2]
        print 'y[3] is', y[3]

        

I can't read every value (one at a time) like that! You are insane, Python.

for-loop is your lazy solution.

list's for-loop

        # Create two lists x and y containing some numbers
        x = [3, 2, 1, 0]

        for value in x:
            print value    # remember indentation (4 space in or a tab in)

        

that's better....

oops.. I solved half of the problem in for1.py for you :(((( finish the rest yourself.

list's for-loop (cont')

        x = [3, 2, 1, 0]

        for <any_var_name> in <da_list>:
            print <any_var_name>    # remember indentation (4 space in or a tab in)

        

you don't have to use value if you don't want to.
any_var_name can be anything like i, my_value, crazy_john, or even cat if you are dealing with a list of cat.

list's for-loop (cont')

          
        x = [3, 2, 1, 0]
             
        for <any_var_name> in <da_list>:
            print <any_var_name>    # remember indentation (4 space in or a tab in)
            
        

da_list can be a pre-defined list (use variable name), or a new list you created on the fly.

     
        for happy in [3, 2, 1, 0]:
            print happy    # remember indentation (4 space in or a tab in)
            
        

your task

finish for1.py, for2.py and for3.py
for3.py asks you to use for-loop to compute the individual sum of x[i] + y[i] where i is the index from 0 to n-1 (both lists are of the same size, n = 4)

needs hint for for3.py?

read x_y_list.py or/and send me email yeukhon@acm.org