Chapter 7 Flashcards
numbers = [1, 2, 3, 4, 5]
numbers[2] = 99
print(numbers)
[1, 2, 99, 4, 5]
numbers = list(range(3))
print(numbers)
[0, 1, 2]
numbers = [10] * 5
print(numbers)
[10, 10, 10, 10, 10]
numbers = list(range(1, 10, 2))
for n in numbers:
print(n)
1
3
5
7
9
numbers = [1, 2, 3, 4, 5]
print(numbers[−2])
4
How do you find the number of elements in a list?
Use the build-in len function
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]
numbers1 = [1, 2, 3]
numbers2 = [10, 20, 30]
numbers2 += numbers1
print(numbers1)
print(numbers2)
[1, 2, 3]
[10, 20, 30, 1, 2, 3]
numbers = [1, 2, 3, 4, 5]
my_list = numbers[1:3]
print(my_list)
[2, 3]
numbers = [1, 2, 3, 4, 5]
my_list = numbers[1:]
print(my_list)
[2, 3, 4, 5] ? [2, 3]
numbers = [1, 2, 3, 4, 5]
my_list = numbers[:1]
print(my_list)
[1]
numbers = [1, 2, 3, 4, 5]
my_list = numbers[:]
print(my_list)
[1, 2, 3, 4, 5]
numbers = [1, 2, 3, 4, 5]
my_list = numbers[−3:]
print(my_list)
[3, 4, 5]
names = [‘Jim’, ‘Jill’, ‘John’, ‘Jasmine’]
if ‘Jasmine’ not in names:
print(‘Cannot find Jasmine.’)
else:
print(“Jasmine’s family:”)
print(names)
Jasmine’s family:
[‘Jim’, ‘Jill’, ‘John’, ‘Jasmine’]
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.
How do you find the lowest and highest values in a list?
You can use the built-in min and max functions.
How do you find the lowest and highest values in a list?
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.
Describe the following list methods:
a. index
b. insert
c. sort
d. reverse
- Index method searches for an item in the list and returns the index of the first element containing that item.
- Insert method inserts an item into the list at a specified index.
- Sort method sorts the items in the list to appear in ascending order.
- Reverse method reverses the order of the items in the list.
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
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]
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]
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.
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]]
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])