Chapter 7 - Intro to Programming
What will the following code display? numbers = list(range(1, 10, 2)) for n in numbers: print(n)
1 3 5 7 9
What will the following code display? numbers = [1, 2, 3, 4, 5] print(numbers[−2])
4
Give two reasons why tuples exist.
Here are three reasons: • Processing a tuple is faster than processing a list, so tuples are good choices when you are processing lots of data and that data will not be modified. • Tuples are safe. Because you are not allowed to change the contents of a tuple, you can store data in one and rest assured that it will not be modified (accidentally or otherwise) by any code in your program. • There are certain operations in Python that require the use of a tuple
What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[1:3] print(my_list)
[2, 3]
What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[−3:] print(my_list)
[3, 4, 5]
What will the following code display? names = ['Jim', 'Jill', 'John', 'Jasmine'] if 'Jasmine' not in names: print('Cannot fnd Jasmine.') else: print("Jasmine's family:") print(names)
Jasmine's family: ['Jim', 'Jill', 'John', 'Jasmine']
Look at the following interactive session, in which a two-dimensional list is created. How many 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.
What is the primary difference between a list and a tuple?
The primary difference between tuples and lists is that tuples are immutable. That means that once a tuple is created, it cannot be changed.
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 a specific value. The del statement removes an element at a specific index.
Look at the following list comprehension: [x for x in my_list] What is the result expression? What is the iteration expression?
The result expression is: x. The iteration expression is: for x in my_list
How do you find the lowest and highest values in a list?
You can use the built-in min and max functions.
Assume the following statement appears in a program: names = [] 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.
What will the following code display? numbers = list(range(3)) print(numbers)
[0, 1, 2]
What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[:] print(my_list)
[1, 2, 3, 4, 5]
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]
What will the following code display? numbers1 = [1, 2, 3] numbers2 = [10, 20, 30] numbers3 = numbers1 + numbers2 print(numbers1) print(numbers2) print(numbers3)
[1, 2, 3] [10, 20, 30] [1, 2, 3, 10, 20, 30]
What will the following code display? numbers = [1, 2, 3, 4, 5] numbers[2] = 99 print(numbers)
[1, 2, 99, 4, 5]
What will the following code display? numbers = [10] * 5 print(numbers)
[10, 10, 10, 10, 10]
After this code executes, what value will the list2 list hold? list1 = [1, 12, 2, 20, 3, 15, 4] list2 = [n for n in list1 if n > 10]
[12, 20, 15]
What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[:1] print(my_list)
[1]
After this code executes, what value will the list2 list hold? list1 = [1, 12, 2, 20, 3, 15, 4] list2 = [n*2 for n in list1]
[2, 24, 4, 40, 6, 30, 8]
What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[1:] print(my_list)
[2, 3,4,5]
Write a set of nested loops that display the contents of the numbers list shown in Checkpoint question 7.22.
for r in range(4): for c in range(2): print(numbers[r][c])
The ______ method searches for an item in the list and returns the index of the first element containing that item.
index
The _______ method inserts an item into the list at a specified index.
insert
Assume my_tuple references a tuple. Write a statement that converts it to a list
my_list = list(my_tuple)
Assume my_list references a list. Write a statement that converts it to a tuple.
my_tuple = tuple(my_list)
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]]
The ______ method reverses the order of the items in the list.
reverse
The _______ method sorts the items in the list to appear in ascending order.
sort
The _____ function tells us the number of elements in a list
the built-in len function