Lists
>>> measurement1 = 43.43 >>> instructors = [„michelle‟, „dan‟, „diane‟,
>>> measurement2 = 109.34 „andrew‟, „giovanna‟]
>>> measurement3 = 123.34 >>> type(instructors)
>>> measurements = [43.43‟, 109.34, 123.34]
>>> type(measurements) >>> type (instructors[2])
>>> measurements[0] >>> student = ['John Reed', 'Trinity College',
43.34 987632345, 3.45]
>>> measurements[1] >>> type(student)
109.34
>>> measurements[2] >>> type (student[0])
123.43
>>> measurements[-1] >>> type (student[2])
123.43
>>> measurements[5] >>> type (student[-1])
o Mutability
>>> id(instructors)
546312
>>> instructors[2] = „jen‟ #mutable
>>> instructors
[„michelle‟, „dan‟, „jen‟, „andrew‟, „giovanna‟]
>>> id(instructors)
5464312
>>> s = “can‟t do this”
>>> s[2] = „x‟ # strings immutable
>>> instructors.append(„diane‟) #changes the list, does not create a new one
>>> instructors
[„michelle‟, „dan‟, „jen‟, „andrew‟, „giovanna‟, „diane‟]
>>> instructors.append(65.43)
>>> instructors
[„michelle‟, „dan‟, „jen‟, „andrew‟, „giovanna‟, „diane‟, 65.43]
>>> id(instructors)
5464312
>>> result = instructors.append(„xxx‟)
print result
None
o Lists and built in functions
>>> len(instructors)
8 #number of strings in the list
>>> measurements
[43.34, 109.34, 123.43]
>>> max(measurements)
123.43
>>> min(instructors)
43.34
>>> max(instructors)
„xxx‟
>>> instructors
['michelle', 'dan', 'jen', 'andrew', 'giovanna', 'diane', 65.43, 'xxx']
>>> min(instructors)
65.43
>>> instructors[-2] = 'paul'
>>> instructors
['michelle', 'dan', 'jen', 'andrew', 'giovanna', 'diane', 'paul', 'xxx']
>>> min(instructors)
'andrew'
>>> max(instructors, measurements) #max of 2 lists return list w/ max first value
['michelle', 'dan', 'jen', 'andrew', 'giovanna', 'diane', 'paul', 'xxx']
>>> sum(measurements)
276.11
>>> sum(instructors)
o Some list methods
>>> L = [„a‟, „b‟, „c‟, „d‟]
>>> L.append(„e‟)
[„a‟, „b‟, „c‟, „d‟, „e‟]
>>> L.insert(2, „new‟)
>>> L
[„a‟, „b‟, „new‟, „c‟, „d‟]
>>> L.insert(len(L), „fun‟]
>>> L
[„a‟, „b‟, „new‟, „c‟, „d‟, „fun‟]
>>> L.insert(-1, „wow‟)
[„a‟, „b‟, „new‟, „c‟, „d‟, „wow‟, „fun‟]
>>> L.insert(999, „www‟) #just a higher number, even out of index
[„a‟, „b‟
More
Less