Py 6
Py 6
Lecture_6
A List is a kind of Collection
• A collection allows us to put many values in a single “variable”
• A collection is nice because we can carry all many values around in
one convenient package.
friends = [ 'Joseph', 'Glenn', 'Sally' ]
5
for i in [5, 4, 3, 2, 1] : 4
print i 3
print ‘Hello!' 2
1
Hello!
Lists and definite loops - best pals
• Just like strings, we can get at any single element in a list using an
index specified in square brackets
>>> x = list()
>>> type(x)<type 'list'>
>>> dir(x)['append', 'count', 'extend', 'index', 'insert', 'pop',
'remove', 'reverse', 'sort']
>>>
Building a list from scratch