Python - HW 7
This will happen if you try to use an index that is out of range for a list.
An IndexError exception will occur.
If you call the index method to locate an item in a list and the item is not found, this happens.
An InvalidIndex exception is raised.
This term refers to an individual item in a list.
Element
A file object's writelines method automatically writes a newline ('n') after writing each list item to the file.
False
Assume list1 references a list. After the following statement executes, list1 and list2 will reference two identical but separate lists in memory: list2 = list1
False
Lists in Python are immutable.
False
You can remove an element from a tuple by calling the tuple's remove method.
False
When the * operator's left operand is a list and its right operand is an integer, the operator becomes this.
The repetition operator
This is the last index in a list.
The size of the list minus one
(SA) What does the following code display? values = [2] * 5 print(values)
[2, 2, 2, 2, 2]
(SA) What will the following code display? values = [2, 4, 6, 8, 10] print(values[1:3])
[4, 6]
What will the following code print? list1 = [40, 50, 60] list2 = [10, 20, 30] list3 = list1 + list2 print(list3)
[40, 50, 60, 10, 20, 30]
(SA) What does the following code display? numbers = [1, 2, 3, 4, 5, 6, 7, 8] print(numbers[−4:])
[5, 6, 7, 8]
(SA) What does the following code display? numbers = [1, 2, 3, 4, 5, 6, 7] print(numbers[5:])
[6, 7]
(SA) Look at the following statement: numbers = [1, 2, 3] a. What value is stored in numbers[2]? b. What value is stored in numbers[0]? c. What value is stored in numbers[−1]?
a. 3 b. 1 c. 3
(SA) Look at the following statement: numbers = [10, 20, 30, 40, 50] 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?
a.5 b.0 c.4
This list method adds an item to the end of an existing list.
append
Assume the following statement appears in a program: mylist = [] Which of the following statements would you use to add the string 'Labrador' to the list at index 0? a. mylist[0] = 'Labrador' b. mylist.insert(0, 'Labrador') c. mylist.append('Labrador') d. mylist.insert('Labrador', 0)
b and c
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.
def list_total(arg): # Set the accumulator to 0. total = 0 # Add the values in arg to total. for val in arg total += val # Return the total. return total
Assume names references a list. Write a for loop that displays each element of the list.
for element in names: print(element)
Assume the list numbers1 has 100 elements, and numbers2 is an empty list. Write code that copies the values in numbers1 to numbers2.
for item in numbers1: numbers2.append(item) or numbers2 = [] + numbers1
Assume the names variable references a list of strings. Write code that determines whether 'Ruby' is in the names list. If it is, display the message 'Hello Ruby'. Otherwise, display the message 'No Ruby'.
if 'Ruby' in names: print('Hello Ruby') else: print('no Ruby')
Write a statement that creates a two-dimensional list with 5 rows and 3 columns. Then write nested loops that get an integer value from the user for each element in the list
mylist = [] for i in range(5): mylist.append([]) for j in range(3): mylist[i].append(None) for i in range (len(mylist)): for j in range (len(mylist[i])): mylist[i][j] = input('Enter a value: ')
Write a statement that creates a list with the following strings: 'Einstein', 'Newton', 'Copernicus', and 'Kepler'.
names = ['Einstein', 'Newton', 'Copernicus', 'Kepler']
This removes an item at a specific index in a list.
the del statement
Which of the following statements creates a tuple?
values = (1,)
This is the first index in a list.
0
A list can be an element in another list.
True
The del statement deletes an item at a specified index in a list.
True
Tuples in Python are immutable.
True
You can use the + operator to concatenate two lists.
True
This is a number that identifies an item in a list.
index
This function returns the length of a list.
len
This built-in function returns the highest value in a list.
max
This file object method returns a list containing the file's contents.
readlines