List, Strings, and Iteration

Lists and Iteration
These notes introduce lists and strings, and explain how to iterate through a list or a string. For the basics on iteration, see iteration 1A

Here is how you create a list of information with Python:


    students = ['bob', 'joe', 'sylvia']


This list has 3 elements, and we can get to each element with an index.

In this case, the valid indexes are 0,1, and 2:

    students[0]     is   'bob'
    students[1]     is   'joe'
    students[2]     is   'sylvia'

If we have such a list, we can iterate through it as in the following code:
 
   students = ['bob', 'joe', 'sylvia']
    i=0
    while i<3:
        print students[i]
        i=i+1
 
What happens if we go to far and try to print a 4th element? Consider the following code:
 
   
students= ['bob', 'joe', 'sylvia','rayray']
    i=0
    while i<3:
        print students[i]
        i=i+1
 
Unfortunately, you'll see this  'index out of range' error a million times in your programming life.
 
Python provides some help-- there is a function that tells you how big a list is. The function is len, and it accepts one parameter which is the list. The following code would return 3 as a result:
 
    len(students)

The function len is very important because it allows us to write code that will work on any list, no matter what its size. Here's another template that you'll use often:

  
students = ['bob', 'joe', 'sylvia'] 
    i=0
   while i<len(students):
        print students[i]
        i=i+1
 
Note that this code will work even if I add more elements to the list 'students', or remove elements.


Question: How would you use iteration to get the sum of a list of numbers?

    list=[3,6,9]









Iterating through a String

A string might be defined as a list of characters, where a character is something found on the keyboard (a letter, digit, punctuation, etc.).

If we want to process each character of a string, we can use a loop similar to the ones described above. For instance, we could count the number of As in a name:

    name = "James Allan"
    i=0
    count=0
    while i<len(name):
        if name[i]=='A' or name[i]=='A':
            count=count+1

          i=i+1

For this string, its length (len) is 11. name[0] is 'J', name[1] is 'a', and so on.

In-Class Problems

1. Open the IDLE programming environment and enter the following code in a file total.py:

    list=[3,6,9]
    i=0
    total=0
    while i<3:
        total=total+list[i]
        i=i+1
    print total

After you copy and paste it, check the indentation and be careful.

a. Run the program. It should print out 18.

b. Add a new element to the list-- change the first line to      list=[3,6,9,12]. Your program should have an error. Fix it so that the program prints 30. Fix it so that the program will work no matter how big the list (hint: use the len function)

2. For this problem, you'll write code for computing grade point average. You'll write it in the IDLE environment, separate from your App Engine dynamic application. After you get it working, you can copy-paste it into that program.

a. Enter the following code into gpa.py and run it.

            grades=["ABCDA"]
            i=0        
           count=0
           while i<len(grades):
                if grades[i]  =='A':
                    count=count+1
                    i=i+1
            print count
   
It should print 2.

b. Modify the program so that it computes the total grade points for the string grade. In this case, it should print 4+3+2+1+4=14

Hint: change the variable named 'count' to total. For each type of grade, add the appropriate number of points to total. You'll need to use if and elif.

c. The grade point average can be computed by dividing the total grade points by the number of grades. Modify your program so that it prints out the gpa for the sample string grades, 2.8.

Hint: You need total to be a floating point number before you divide it by the # of grades. You can use the function float(total) to get the floating point version of the total.

Once you have this code working, you can copy-paste it into your web program.

Recent site activity