I Hate CBT's

View Original

7.2.9 Teacher Class List Methods

Question: 7.1 What will the following code display?
numbers = [1, 2, 3, 4, 5]
numbers[2] = 99
print(numbers)
Answer: [1, 2, 99, 4, 5]

Because the second index is "3" and we're replacing by 99
==================================================
Question: 7.2 What will the following code display?
numbers = list(range(3))
print(numbers)
Answer: [0, 1, 2]
Because range 3 means 0 to 2 will be in the list
==================================================
Question: 7.3 What will the following code display?
numbers = [10] * 5
print(numbers)
Answer: [10, 10, 10, 10, 10]
Because we're multiplying the list by 5
==================================================
Question: 7.4 What will the following code display?
numbers = list(range(1, 10, 2))
for n in numbers:
print(n)
Answer: 1
3
5
7
9
Because first of all numbers will be [1, 3, 5, 7, 9] because we start at 1, end at 10 (9)
n is the interaction of the loop so each time the loop is performed it will print that number.
==================================================
Question: 7.5 What will the following code display?
numbers = [1, 2, 3, 4, 5]
print(numbers[−2])
Answer: 4
Because u count backward 5 is -1, -2 is 4
==================================================
Question: 7.6 How do you find the number of elements in a list?
Answer: Use the len function
==================================================
Question: 7.8 What will the following code display?
numbers1 = [1, 2, 3]
numbers2 = [10, 20, 30]
numbers2 += numbers1
print(numbers1)
print(numbers2)
Answer: [1, 2, 3]
[10, 20, 30, 1, 2, 3]
It has to be in that order because you're adding number one TO number two
==================================================
Question: 7.9 What will the following code display?
numbers = [1, 2, 3, 4, 5]
my_list = numbers[1:3]
print(my_list)
Answer: [2, 3]
Because 2 is the first index, 3 because end is 3 and 4 is the 3rd index but end is the index marking the end of the slice meaning we don't include it
==================================================
Question: 7.10 What will the following code display?
numbers = [1, 2, 3, 4, 5]
my_list = numbers[1:]
print(my_list)
Answer: [2, 3, 4, 5] because we start at index one which is 2 and everything after
==================================================
Question: 7.11 What will the following code display?
numbers = [1, 2, 3, 4, 5]
my_list = numbers[:1]
print(my_list)
Answer: [1] because it's everything before, not including the first index
==================================================
Question: 7.12 What will the following code display?
numbers = [1, 2, 3, 4, 5]
my_list = numbers[:]
print(my_list)
Answer: [1, 2, 3, 4, 5] because it's printing all the items
==================================================
Question: 7.13 What will the following code display?
numbers = [1, 2, 3, 4, 5]
my_list = numbers[−3:]
print(my_list)
Answer: [3, 4, 5] because we start at -3 from the end so 5, 4, 3 then a=everything after that since its on the left side; 3, 4, 5
==================================================
Question: 7.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:")
print(names)
Answer: Jasmine's family:
['Jim', 'Jill', 'John', 'Jasmine']

because jasmine is in the list
==================================================
Question: 7.15 What is the difference between calling a list’s remove method and using the del statement to remove an element?
Answer: The remove method you saw earlier removes a specific item from a list, if that item is in the list. Some situations might require you remove an element from a specific index, regardless of the item that is stored at that index. This can be accomplished with the del statement.
==================================================
Question: 7.17 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?

or
names.append('Wendy')
Answer: 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
==================================================
Question: 7.16 How do you find the lowest and highest values in a list?
Answer: You can use the built-in min and max functions.
min function accepts a sequence, such as a list, as an argument and returns the item that has the lowest value in the sequence. Here is an example:
my_list = [5, 4, 3, 2, 50, 40, 30]
print('The lowest value is', min(my_list))
This code will display the following:
The lowest value is 2
The max function accepts a sequence, such as a list, as an argument and returns the item that has the highest value in the sequence. Here is an example:
my_list = [5, 4, 3, 2, 50, 40, 30]
print('The highest value is', max(my_list))
This code will display the following:
The highest value is 50
==================================================
Question: This term refers to an individual item in a list.
a. element
b. bin
c. cubbyhole
d. slot
Answer: element
==================================================
Question: This is a number that identifies an item in a list.
a. element
b. index
c. bookmark
d. identifier
Answer: index
==================================================
Question: This is the first index in a list.
a. - 1
b. 1
c. 0
d. The size of the list minus one
Answer: 0
==================================================
Question: This is the last index in a list.
a. 1
b. 99
c. 0
d. The size of the list minus one
Answer: The size of the list minus one
==================================================
Question: 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 list will be erased and the program will continue to run.
d. Nothing-the invalid index will be ignored.
Answer: An IndexError exception will occur.
==================================================
Question: This function returns the length of a list.
a. length
b. size
c. len
d. lengthof
Answer: len
==================================================
Question: When the * operator's left operand is a list and its right operand is an integer, the operator becomes this.
a. The multiplication operator
b. The repetition operator
c. The initialization operator
d. Nothing-the operator does not support those types of operands.
Answer: The repetition operator
==================================================
Question: This list method adds an item to the end of an existing list.
a. add
b. add to
c. increase
d. append
Answer: append
==================================================
Question: 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
Answer: the del statement
==================================================
Question: 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?

b. mylist.insert(0, 'Labrador')
c. mylist. append ( 'Labrador' )
d. mylist.insert('Labrador', 0)
Answer: mylist.insert(0, 'Labrador')
==================================================
Question: 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 Invalidindex exception is raised.
c. The method returns - 1.
d. Nothing happens. The program continues running at the next statement.
Answer: A ValueError exception is raised.
==================================================
Question: This built-in function returns the highest value in a list.
a. highest
b. max
c. greatest
d. best of
Answer: max
==================================================
Question: This file object method returns a list containing the file's contents.
a. to_list
b. getlist
c. readline
d. readlines
Answer: readlines
==================================================
Question: Lists in Python are immutable.

T or F
Answer: False
==================================================
Question: The del statement deletes an item at a specified index in a list.

T or F
Answer: True
==================================================
Question: Assume list1 references a list. After the following statement executes, list1 and list2 will reference two identical but separate lists in memory:
list2 = list1

T or F
Answer: False
==================================================
Question: A file object's writelines method automatically writes a newline ( '\n' ) after writing each list item to the file.

T or F
Answer: False
==================================================
Question: You can use the + operator to concatenate two lists.

T or F
Answer: True
==================================================
Question: A list can be an element in another list.

T or F
Answer: True
==================================================
Question: Sequence
Answer: an object that holds multiple items of data, stored one after the other. You can perform operations on a sequence to examine and manipulate the items stored in it.
==================================================
Question: List
Answer: An object that contains multiple data items.
Lists are mutable, which means that their contents can be changed during a program's execution.
==================================================
Question: Repetition Operator
Answer: The repetition operator makes multiple copies of a list and joins them all together.
The star symbol is used, when the operand on the left side of the * symbol is a sequence (such as a list) and the operand on the right side is an integer, it becomes a repetition operator.
==================================================
Question: len Function
Answer: a built-in function that returns the length of a sequence, such as a list.
==================================================
Question: Mutable
Answer: means their elements can be changed
==================================================
Question: How do you find the number of elements in a list?
Answer: By using the len Function.
The len function takes the list as an argument and it returns the number of the elements present in the list .
==================================================
Question: slice
Answer: is a span of items that are taken from a sequence. A slicing expression selects a range of elements from a sequence.
==================================================
Question: in Operator
Answer: can be used to determine whether an item is contained in a list.
==================================================
Question: append(item) Method
Answer: commonly used to add items to a list. The item that is passed as an argument is appended to the end of the list's existing elements.
==================================================
Question: index( item) Method
Answer: Returns the index of the first element whose value is equal to item.
A ValueError exception is raised if item is not found in the list.
==================================================
Question: insert(index, item) Method
Answer: Inserts an item into the list at the specified index.
==================================================
Question: sort() Method
Answer: Sorts the items in the list so they appear in ascending order (from the lowest value to the highest value).
==================================================
Question: remove(item) Method
Answer: Removes the first occurrence of the item from the list.
A ValueError exception is raised if the item is not found in the list.
==================================================
Question: reverse() Method
Answer: Reverses the order of the items in the list.
==================================================
Question: del Statement
Answer: removes an element from a specific index, regardless of the item that is stored at that index.
==================================================
Question: min Function
Answer: The min function accepts a sequence, such as a list, as an argument and returns the item that has the lowest value in the sequence.
==================================================
Question: max Function
Answer: The max function accepts a sequence, such as a list, as an argument and returns the item that has the highest value in the sequence.
==================================================
Question: What is the difference between calling a list's remove method and using the del statement to remove an element?
Answer: the remove method is used to remove a specific element in a list.
the del method is used to remove any element that is present at a specific index, regardless of what that element is.
==================================================
Question: How do you find the lowest and highest values in a list?
Answer: by using the min and/or the max functions
==================================================
Question: two-dimensional list (nested lists)
Answer: a list that has other lists as its elements.
==================================================
Question: Look at the following statement:
numbers = [10, 20, 30, 40, 50]
How many elements does the list have?
What is the index of the first element in the list?
What is the index of the last element in the list?
Answer: a. 5
b. 0
c.4
==================================================
Question: Look at the following statement:
numbers = [1, 2, 3]
What value is stored in numbers[2]?
What value is stored in numbers[0]?
What value is stored in numbers[−1]?
Answer: a. 3
b. 1
c. 3 (-1 is the last item)
==================================================
Question: What will the following code display?
values = [2, 4, 6, 8, 10]
print(values[1:3])
Answer: [4, 5]
==================================================
Question: What does the following code display?
numbers = [1, 2, 3, 4, 5, 6, 7]
print(numbers[5:])
Answer: [6, 7]
==================================================
Question: What does the following code display?
numbers = [1, 2, 3, 4, 5, 6, 7, 8] print(numbers[−4:])
Answer: [5, 6, 7, 8]
==================================================
Question: What does the following code display?
values = [2] * 5
print(values)
Answer: [2, 2, 2, 2, 2]
==================================================