2Sequences: Lists and Tuples
Note: Throughout the Instructor Solutions Manual, solutions are not provided for project,
research and challenge exercises—many of which are substantial and appropriate for term
projects, directed-study projects, capstone-course projects and thesis topics. Before assign
ing a particular exercise for homework, instructors should check the IRC to be sure the
Exercises
Use IPython sessions for each exercise where practical.
5.1 (What’s Wrong with This Code?) What, if anything, is wrong with each of the fol-
lowing code segments?
a) day, high_temperature = (‘Monday’, 87, 65)
b) numbers = [1, 2, 3, 4, 5]
numbers[10]
Answer: The index 10 is not within the list’s bounds. In this case, valid indices are
c) name = ‘amanda’
name[0] = ‘A’
Answer: Strings are immutable so you cannot assign99ikjm to the elements in a
d) numbers = [1, 2, 3, 4, 5]
e) student_tuple = (‘Amanda’, ‘Blue’, [98, 75, 87])
student_tuple[0] = ‘Ariana’
f) (‘Monday’, 87, 65) + ‘Tuesday
g) ‘A’ += (‘B’, ‘C’)
h) x = 7
del x
print(x)
i) numbers = [1, 2, 3, 4, 5]
numbers.index(10)
PyCDS_05_ListsTuples.fm Page 2 Tuesday, May 21, 2019 12:03 PM