After Mid
After Mid
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()
Chapter 8
More About Strings
Method Description
isalnum() Returns true if the string contains only alphabetic letters or digits and is at least one
character in length. Returns false otherwise.
isalpha() Returns true if the string contains only alphabetic letters and is at least one
character in length. Returns false otherwise.
isdigit() Returns true if the string contains only numeric digits and is at least one character
in length. Returns false otherwise.
islower() Returns true if all of the alphabetic letters in the string are lowercase, and the string
contains at least one alphabetic letter. Returns false otherwise.
isspace() Returns true if the string contains only whitespace characters and is at least one
character in length. Returns false otherwise. (Whitespace characters are spaces,
newlines (\n), and tabs (\t).
isupper() Returns true if all of the alphabetic letters in the string are uppercase, and the string
contains at least one alphabetic letter. Returns false otherwise.
Method Description
lower() Returns a copy of the string with all alphabetic letters converted to lowercase. Any
character that is already lowercase, or is not an alphabetic letter, is unchanged.
lstrip() Returns a copy of the string with all leading whitespace characters removed. Leading
whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the
beginning of the string.
lstrip(char) The char argument is a string containing a character. Returns a copy of the string with all
instances of char that appear at the beginning of the string removed.
rstrip() Returns a copy of the string with all trailing whitespace characters removed. Trailing
whitespace characters are spaces, newlines (\n), and tabs (\t) that appear at the end of
the string.
rstrip(char) The char argument is a string containing a character. The method returns a copy of the
string with all instances of char that appear at the end of the string removed.
strip() Returns a copy of the string with all leading and trailing whitespace characters removed.
strip(char) Returns a copy of the string with all instances of char that appear at the beginning and
the end of the string removed.
upper() Returns a copy of the string with all alphabetic letters converted to uppercase. Any
character that is already uppercase, or is not an alphabetic letter, is unchanged.
Method Description
endswith(substring) The substring argument is a string. The method returns true if the
string ends with substring.
find(substring) The substring argument is a string. The method returns the
lowest index in the string where substring is found. If substring
is not found, the method returns −1.
replace(old, new) The old and new arguments are both strings. The method returns a
copy of the string with all instances of old replaced by new.
startswith(substring) The substring argument is a string. The method returns true if the
string starts with substring.
– This string contains the tokens 17, 92, 81, 12, 46, and 5
– The delimiter is the ; character
Chapter 9
Dictionaries and Sets
Method Description
Clear Clears the contents of a dictionary.
get Gets the value associated with a specified key. If the key is not found, the method
does not raise an exception. Instead, it returns a default value.
items Returns all the keys in a dictionary and their associated values as a sequence of
tuples.
keys Returns all the keys in a dictionary as a sequence of tuples.
pop Returns the value associated with a specified key and removes that key-value pair
from the dictionary. If the key is not found, the method returns a default value.
popitem Returns, as a tuple, the key-value pair that was last added to the dictionary. The
method also removes the key-value pair from the dictionary.
values Returns all the values in the dictionary as a sequence of tuples.
Chapter 10
Classes and Object-Oriented
Programming
Figure 10-2 Code outside the object interacts with the object’s methods
Figure 10-4 The housefly and mosquito objects are instances of the Insect class
Figure 10-7 The coin1, coin2, and coin3 variables reference three Coin objects
Chapter 11
Inheritance