- How to Program, Part I
- How to Program, Part II
- How to Program, Part III
- How to Program, Part IV
- How to Program, Part V
- exercises
- pyMPI tutorial
- Calculating PI, Part I
- Calculating PI, Part II
- Calculating PI, Part III
- Poogle - Web Search
- Mandelbrot Sets
- Mandelbrot, The Code
- Mandelbrot, The Images
- Conway's Life, Part I
- Life Code Listing
- Conway's Life, Part II
- MPI Life Code Listing
| How to Program, Part III
This lesson is about lists. You can think of lists as being like shelves. The program
file: list1.py |
1 | a = ["apple","banana","orange"] |
2 | print a |
> python list1.py
['apple', 'banana', 'orange']
|
creates an list with three elements, each of which is a string. Think of the comma separated list of items inside the square brackets as a kind of storage case. Each item on the list is in its own shelf, which has a number. The apple is on shelf 0, the banana is on shelf 1, and the orange is on shelf 2.
We can use this number to find out whats on a shelf, or to replace the contents of a shelf.
file: list2.py |
1 | a = ["apple","banana","orange"] |
2 | print a |
3 | a[1] = "pear" # replaces the contents of shelf one |
4 | print a |
5 | print a[0] # prints the contents of shelf zero |
> python list2.py
['apple', 'banana', 'orange'] ['apple', 'pear', 'orange'] apple
|
The third line of the program replaces the contents of shelf 1 with "pear." The last line of the program prints out the contents of shelf 0.
If we want, we can make our shelf unit larger.
file: list3.py |
1 | a = [6,9,7] |
2 | a.append(8) # Adds a new shelf. It will be number 3. |
3 | print a |
> python list3.py
[6, 9, 7, 8]
|
The "append" method increased the size of the shelf, and puts "8" into the new shelf.
We can shorten this example by using the "+" sign to join two lists
together:
file: list3v2.py |
1 | a = [6,9,7] |
2 | a = a + [8] # Adds a new shelf. It will be number 3. |
3 | print a |
> python list3v2.py
[6, 9, 7, 8]
|
Or if we like, we can use the super short-hand of saying "a +=". It
means the same thing as "a = a +".
file: list3v3.py |
1 | a = [6,9,7] |
2 | a += [8] # Adds a new shelf. It will be number 3. |
3 | print a |
> python list3v3.py
[6, 9, 7, 8]
|
You can print the elements of your shelf in order by doing this:
file: list4.py |
1 | a = ["meat","fruit","veggies"] |
2 | for v in a: |
3 | print v |
> python list4.py
meat fruit veggies
|
Try it!
Now we can change things up and print each item in the list with a number next to it:
file: list5.py |
1 | a = ["meat","fruit","veggies"] |
2 | for i in range(len(a)): |
3 | print i,"]",a[i] |
> python list5.py
0 ] meat 1 ] fruit 2 ] veggies
|
The function "len" tells you the length of an list. Thus "len(a)" is 3.
If you haven't guessed, the function "range" creates an list of integers.
file: prange.py |
1 | print range(10) |
> python prange.py
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
Perhaps less obviously, strings are lists of characters:
file: strischars.py |
1 | for c in "wow": |
2 | print c |
> python strischars.py
w o w
|
One of the cool things about lists is that you can nest one inside the other
file: list6.py |
1 | tictactoe = [ |
2 | [ 'X', '-', 'O' ], |
3 | [ 'O','X','-' ], |
4 | [ '-' ,'O','X']] |
5 | |
6 | # now count the number of X's and O's |
7 | # on the board and display the result |
8 | xs = 0 |
9 | os = 0 |
10 | for row in tictactoe: |
11 | for col in row: |
12 | if col == 'X': |
13 | xs = xs + 1 |
14 | elif col == 'O': |
15 | os = os + 1 |
16 | print "xs=",xs," os=",os |
> python list6.py
xs= 3 os= 3
|
The above example of a list of lists shows you how you might represent the game board for tic tac toe. Try changing the placement of X's and O's on the board, and check that the counts come out right. There are many uses for lists, and you can nest them as deeply as you want. You can have a list of lists of lists of lists of lists.
Note that because strings are just lists of characters, this works the
same way:
file: list6v2.py |
1 | tictactoe = [ |
2 | "X-O", |
3 | "OX-", |
4 | "-OX"] |
5 | |
6 | # now count the number of X's and O's |
7 | # on the board and display the result |
8 | xs = 0 |
9 | os = 0 |
10 | for row in tictactoe: |
11 | for col in row: |
12 | if col == 'X': |
13 | xs = xs + 1 |
14 | elif col == 'O': |
15 | os = os + 1 |
16 | print "xs=",xs," os=",os |
> python list6v2.py
xs= 3 os= 3
|
| |