07 - Gaddis Python - Lecture - PPT - ch07
07 - Gaddis Python - Lecture - PPT - ch07
Chapter 7
Lists and Tuples
Method Description
append(item) Adds item to the end of the list.
index(item) Returns the index of the first element whose value is equal to item.
A ValueError exception is raised if item is not found in the list.
insert(index, item) Inserts item into the list at the specified index. When an item is inserted into a
list, the list is expanded in size to accommodate the new item. The item that
was previously at the specified index, and all the items after it, are shifted by
one position toward the end of the list. No exceptions will occur if you specify an
invalid index. If you specify an index beyond the end of the list, the item will be
added to the end of the list. If you use a negative index that specifies an invalid
position, the item will be inserted at the beginning of the list.
sort() Sorts the items in the list so they appear in ascending order (from the lowest
value to the highest value).
remove(item) Removes the first occurrence of item from the list. A ValueError exception
is raised if item is not found in the list.
reverse() Reverses the order of the items in the list.
list1 = [1, 2, 3, 4]
list2 = [item**2 for item in list1]
for n in list1:
if n < 10:
list2.append(n)
For more information about the import statement, see Appendix E in your textbook.
Displayed by the
ylabel() function.
Displayed by the
xticks() function. Displayed by the
xlabel() function.
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved. 7 - 45
Plotting a Bar Chart (1 of 6)
• Use the bar function in the matplotlib.pyplot
module to create a bar chart.
• The function needs two lists: one with the X
coordinates of each bar’s left edge, and another with
the heights of each bar, along the Y axis.
plt.bar(left_edges, heights)
plt.show()