Review Questions

Multiple Choice

1. This term refers to an individual item in a list.

2. This is a number that identifies a character in a string, or an item in a list.

a. element b. index c. bookmark d. identifier

3. This is the first index in a string or a list.

d. The size of the list minus one

4. This is the last index in a string or a list.

d. The size of the string or list minus one

5. This will happen if you try to use an index that is out of range for a list.

a. a ValueError exception will occur b. an IndexError exception will occur c. The string or list will be erased and the program will continue to run.

d. Nothing—the invalid index will be ignored

6. This function returns the length of a string or a list.

7. This string method returns a copy of the string with all leading whitespace characters removed.

a. lstrip b. rstrip c. remove d. strip_leading

8. This string method returns the lowest index in the string where a specified substring is found, a. first_index_of b. locate c. find d. index of k

9. This list method adds an item to the end of an existing list.

10. This removes an item at a specific index in a list.

a. The remove method b. The delete method c. The del statement d. The kill method

11. Assume the following statement appears in a programt my list = [ ]

Which of the following statements would you use to add the string 'Labrador' to the list at index 0?

c. mylist.append{'Labrador')

d. mylist.insert('Labrador', 0)

12. If you call the index method to locate an item in a list and the item is not found, this happens.

a. A ValueError exception is raised b. An Invalidlndex exception is raised c. The method returns -1

d. Nothing happens. The program continues running at the next statement.

13. This built-in function returns the highest value in a list.

a. highest b. max c. greatest d. best_of

14. This file object method returns a list containing the file's contents.

a. to_list b. getlist c. readline d. readlines

15. Assume the following statement appears in a program: mylist = 'four score and seven'

Which of the following expressions would you use to get the following list? ['four', 'score', 'and1, 'seven']

d. mylist.getlist()

True or False

1. Strings in Python are immutable.

2. Lists in Python are immutable.

3. The del statement deletes an item at a specified index in a list.

4. Assume listl references a list. After the following statement executes, listl and list2 will reference two identical bur separate lists in memory:

list2 = listl

5. A file object's writelines method automatically writes a newline ( ' \n' ) after writing each list item ro the file.

Short Answer

1. Look at the following statement:

a. How many elements does the list have?

b. What is the index of the first element in the list?

c. What is the index of the last element in the list?

3. L ook at the following statement: numbers = [1, 2, 3 ]

4. What will the following code display? mystring = 'abcdefg'

print mystring[2:5]

5. What does the following code display?

6. What does the following code display?

name = 'joe' print name.lower() print name.upper() print name

Algorithm Workbench

1. Assume choice references a string. The following if statement determines whether choice is equal to 'Y* or 'y';

Rewrite this statement so it only makes one comparison and does not use the or operator, {Hint: use either the upper or lower methods.)

2. Write a loop that counts the number of space characters that appear in the string referenced by mystring.

3. Write a loop that counts the number of digits that appear in the srring referenced by mystring.

4. Write a loop that counts the number of lowercase characters that appear in the string referenced by mystring.

5. Write a function that accepts a string as an argument and returns true it the argument ends with the substring ■ .com'. Otherwise, the function should return false.

6. Write code that makes a copy of a string with ail occurrences ofr the lowercase letter 11 * converted to uppercase.

7. Write a function that accepts a string as an argument and displays the string backwards.

8. Assume mystring references a string. Write a print statement that uses a slicing expression to display the first 3 characters in the string.

9. Assume mystring references a string. Write a print statement that uses a slicing expression to display the last 3 characters in the string.

10. Write a statement that creates a list with the following strings: 'Einstein', 'Newton', 1 Copernicus',and 'Kepler'.

11. Assume names references a list. Write a for loop that displays each element of the list.

12. Assume the lists numbers 1 has 100 elements and numbers2 is an empty list. Write code that copies the values in numbers 1 to numbers2.

13. Draw a flowchart showing the general logic for totaling the values in a list.

14. Write a function that accepts a list as an argument (assume the list contains integers) and returns the total of the values in the list.

15. Look at the following statement:

mystring = 'cookies>milk>fudge>cake>ice cream' Write a statement that splits this string, creating the following list: ['cookies', 'milk', 'fudge', 'cake', 'ice cream']

+1 0

Post a comment