Checkpoint Chapter 7
13. What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers [-3:] print (my_list)
[3, 4, 5]
9. What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers [1:3] print (my_list)
[2, 3]
5. What will the following code display? numbers = [1, 2, 3, 4, 5] print (numbers[-2])
4
14.What will the following code display? names = ['Jim, 'Jill, 'John, 'Jasmine''] if 'Jasmine' not in names: print ('cannot find Jasmine.') else: print ("Jasmine's family:")
Jasmine's family: ['Jim', 'Jill', 'John', 'Jasmine']
4. What will the following code display? numbers = list (range(1, 10, 2)) for n in numbers: print (n)
1 3 5 7 9
look at the following interactive session, in which a two-dimensional list is created. How may rows and how many columns. are in the list? numbers = [[1, 2], [10, 20], [100, 200], [1000, 2000]]
The list contains 4 rows and 2 columns.
15. What is the difference between calling a list's remove method and using the del statement to remove an element?
The remove method searches for and removes an element containing specific value. The del statement remove an element at a specific index.
6. How do you find the number elements in a list?
Use the built-in len function.
16. How do you find the lowest and highest values in a list?
You can use the built-in min and max functions.
17. Assume the following statement appears in a program: name = [ ] Which of the following statements would you use to add the string 'wendy' to the list at index 0? Why would you select this statement instead of the other? a. names [0] = 'wendy' b. names. append(wendy)
You would use statement b, names.append('wendy'). This is because element 0 does not exist. If you try to use statement a, an error will occur.
2. What will the following code display? numbers = list (range(3) ) print (numbers)
[0, 1, 2]
12. What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers [:] print (my_list)
[1, 2, 3, 4, 5, ]
8. What will the following code display? numbers1 = [1, 2, 3,] numbers2 = [10, 20, 30] numbers2 += numbers1 print (numbers1) print (numbers2)
[1, 2, 3] [10, 20, 30 1, 2, 3]
7. What will the following code display? numbers1 = [1, 2, 3,] numbers2 = [10, 20, 30] numbers3 = numbres1 + numbers2 print (numbers1) pritn (numbers2) print (numbers3)
[1, 2, 3] [10, 20, 30] [1, 2, 3, 10, 20, 30]
1. What will the following code display? numbers = [1, 2, 3, 4, 5] numbers [2] = 99 print (numbers)
[1, 2, 99, 4, 5]
3. What will the following code display? number = [10] * 5 print (numbers)
[10, 10, 10, 10, 10]
11. What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers [:1] print (my_list)
[1]
10. What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers [1:] print (my_list)
[2, 3]
Describe the following list methods: a. index b. insert c. sort d. reverse
a) The index method searches for an item in the list and returns the index of the first element containing that item. b) The insert method inserts an item into the list at a specified index. c) The sort method sorts the items in the list to appear in ascending order. d) The revers method reverse the order of the items in the list.
Write a set of nested loops that display the contents of the numbers list shown in checkpoint question 7.19.
for r in range (4): for c in range (2): print ( numbers [r] [c] )
Write a statement that creates a two-dimensional list with three rows and four columns. Each element should be assigned the value 0.
mylist = [0, 0, 0, 0, ], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]