[prog] I don't understand this program (-- python) how
does this work??
Dan Richter
daniel.richter at wimba.com
Fri Dec 13 12:23:45 EST 2002
Hi there.
It looks to me like your basic problem involves understanding lists and
indexes.
Consider this program:
my_list = ['foo', 'bar', 'baz']
print my_list[0] # Output is 'foo'.
print my_list[1] # Output is 'bar'.
print my_list[2] # Output is 'baz'.
In the last three lines, the number between the brackets is called an
index. As Robert pointed out, in Python the first element of a list has
index zero, the second index one, etc.
Now we're going to do the same thing using a variable instead of a constant
index:
my_list = ['foo', 'bar', 'baz']
my_index = 0
while my_index < len(my_list):
print my_list[my_index]
my_index = my_index + 1
This program outputs 'foo', then 'bar', the 'baz'.
Now consider a list of lists:
list_of_lists = [ ['a','b','c'], ['d','e','f'] ]
thing = list_of_lists[0]
# In other words, thing = ['a','b','c']
print thing[0] # Output is 'a'.
other_thing = list_of_lists[1]
# In other words, other_thing = ['d','e','f']
print other_thing[0] # Output is 'd'.
I hope this helps.
========== Dan Richter ============== mailto:Dan at wimba.com ===========
More information about the Programming
mailing list