1/198
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What is a List?
A data structure that can store many values of under one name
Which is an example of how to create an empty list?
Variable = []
Variable = list()
Which is an example of how to create a list with predefined values?
colors = ["red, "yellow", "blue"]
To create a list with predefined values, you have to....
Put a sequence of items in []
What does list1.append(elem) do?
Adds a single element to the end of the list
What does list1.insert(index, elem) do?
Inserts the element elem at the given index, shifting elements to the right
What does list1.extend(list2) do?
Add the elements in list2 to the end of the list.
T or F: The extend method is not only used with a list.
False
T or F: Using + or += on a list is similar to using the extend method.
True
How do you access the individual element in a list using an index?
list1[index]
What are characteristics of Positive Indexes?
First element in the list has index 0, second element has index 1, and n'th element is n-1
What are characteristics of Negative Indexes?
Identify positions relative to the end of the list, index -1 identifies the last element, and -2 identifies the next to last element, etc.
What does Function do?
Is a description
What does len do?
Returns the number of items in the list
What does min do?
Returns the minimum among the elements in the list
What does max do?
Returns the maximum among the elements in the list
What does sum do?
Returns the sum of all the elements in the list. (applies to list of numerical values only)
Given a list, you may use ________ to randomize the order of the elements in a list.
Shuffle
What do you type to import the shuffle function and use the shuffle function with a list?
from random import shuffle
shuffle(list1)
You can refer to elements of a list using the...
Subscript syntax
T or F: The code below makes third have a value of 83.0
scores = [75.0, 68.5, 83.0]
third = scores[2]
True
T or F: Negative indices count from the right end.
True
What is the value of third after running the code below?
scores = [75.0, 68.5, 83.0]
third = scores[-1]
83.0
Which is an example of negative indices?
scores = [75.0, 68.5, 83.0]
third = scores[-1]
Which is an example of positive indices?
scores = [75.0, 68.5, 83.0]
third = scores[2]
Slicing gives a...
List of elements
Subscripting gives a...
Single element
Which is an example of slicing a list?
scores = [68.5, 83.0]
exams = scores[1:3]
T or F: Each list is an object.
True
Which is an example of how you call a list method?
list.methodName(arguments)
T or F: The in operator does not check for that exact method, and looks "inside" elements.
False
To search for an element in a list, you can use the...
in operator and index method
Which is an example of using the in operator to search for an element in a list?
Names = ["William", "Scott", "Sophia"]
if "William" in Names:
Which is an example of using the index method to search for an element's position in a list?
Names = ["William", "Scott", "Sophia"]
loc = Names.index("Scott")
# loc is 1
Which is an example of using the index method to search for an element from the start to the end of a list?
List.index(elem, start, end)
T or F: If the index method finds the element, it return its index (position) inside the list.
True
T or F: A ValueError is given when the index method searches for the element and the element is not between start and end in the list.
True
T or F: A Searchgive Error is not given when the index method is used and the element is NOT found.
False
Which is an example of transversing a list with a for loop?
scores = [ 85, 72, 56, 98, 84, 72]
for grade in scores:
print(grade)
To get both indices and elements in transversing a list, use...
A loop controlled with range(len(list)) with subscripting
for index in range(len(scores)):
print (index,"\t"
,grade[i])
Enumerate(mylist) to control the
for loop
for index, grade in enumerate(scores):
print (index,"\t"
,grade)
Which is an example of using the extend method to add a whole list to the end of a list?
scores = [ 85, 72, 56, 98, 84, 72]
scores.extend([55, 88, 79])
What will be the result of the code below?
Names = ["William", "Scott", "Sophia"]
Names.insert (2, "Hanah")
Names = ["William", "Scott", "Hannah", "Sophia"]
T or F: Append, extend, and insert return something.
False
What will be the result of this code?
colors = colors.append("yellow")
ERROR
colors = None
What will be the result of this code?
colors.append("yellow")
Changes colors to have "yellow" at end
What will be the result of this code?
colors + primaries
ERROR
Longer list will be thrown out
What will be the result of this code?
colors = colors + primaries
Returns a new list
T or F: You can delete from a list by index.
True
Which is an example of deleting from a list using index?
del list[index]
Deleting from a list using index...
Removes the element at position index and shifts down the following elements to fill in the gap
Which is an example of deleting a range from a list by using a slice?
del list[2 : 5]
# removes elements at positions 2, 3, and 4
What does colors.remove("blue") do?
Searches for the first occurrence of "blue and deleters it
What does the "search and destroy" method do?
Searches for and removes a specific value from a list
What happens if the specific value can not be found using the "search and destroy" method?
Gives a runtime error
Which is an example fo the "search and destroy" method using del?
if "blue" in colors:
pos =
colors.index("blue")
del colors[pos]
What does the reverse method do to a list?
Reverses the order of a list
Which is an example of using the reverse method on a list?
mylist = ["red", "green", "blue"]
mylist.reverse()
print(mylist) gives ["blue", "green", "red"]
T or F: After using the reverse method on a list, the original order is lost.
True
T or F: The reverse method does not return a value.
True
What will be the result of this code?
mylist = mylist.reverse()
ERROR
mylist becomes None
Which is an example of a new reversed copy of the list? (original list remains unchanged)
backwards = list(reversed(mylist))
Reverse is a....
Method that mutates the list that is its argument
Reversed is a....
Function that returns a new sequence (Does not actually return a list, but you can convert it with the list typecast)
You can sort a list using the...
Sort method
The default to the sort method being used on a list with integers is...
Ascending order
Which is an example of the sort method being used on a list with integers?
scores = [ 75, 63, 92 ]
scores.sort()
print(scores)
# gives [63, 75, 92]
The default to the sort method being used on a list with strings is...
Alphabetic (ASCII) order
Which is an example of the sort method being used on a list with strings?
physicists = ["Einstein", "Newton", "Hawking"]
physicists.sort()
print(physicists)
# gives ["Einstein", "Hawking", "Newton"]
Which is an example of the sort method being used on a list with integers to sort it by descending order?
scores.sort(reverse = True)
print(scores)
# gives [92, 75, 63]
Which is an example of making a new sorted list and keeping the original list as well?
ordered_list = sorted(scores)
Which are different ways to create a list?
Hard code it, start out with an empt list and append it, start out with and empty list and concatenate to it, split a string, and/or replication
Which is the output of this code?
colors = ["red", "green", "blue"]
colors[0] = "purple"
colors = ["purple", "green", "blue"]
Which is the output of this code?
colors = ["red", "green", "blue"]
colors[3] = "yellow"
IndexError: list assignment is out of range
(Is the result if an index is greater than or equal to the length of the list)
T or F: It is not possible to have two variables referring to the very same list (aliasing)
False
How can two variables refer to the same list?
Arguments and parameters or by assigning one to the other
Which is an example of two variables referring to the same list?
testscores = [ 84, 100, 78]
myscores = testscores
# alias!
myscores.append(96)
print(testscores)
#gives[ 84, 100, 78, 96]
Functions which take lists as arguments and change them during execution are called __________.
Modifiers
The changes that functions make after taking lists as arguments and changing them during execution are called __________
Side Effects
Passing a list as an argument actually ______________ , not a copy of the list.
Passes a reference to the list
T or F: Lists are mutable.
True
T or F: Since lists are mutable, changes made to the elements referenced by the parameter change the same list that the argument is referencing
True
Which is an example of passing a list as an argument?
def doubleValues(aList):
#double its values in the list
for position in range(len(aList)):
aList[position] = 2 * aList[position]
numList = [2, 5, 9]
print(numList)
doubleValues(numList)
print(numList)
What is Scaling?
Multiplying all elements by the same number
What is Parameters?
A list and a scaling factor
What is Postconditions?
Mutates the list and returns nothing
T or F: Usually a mutating function needs to loop over elements, not indexes
False
T or F: Pretty much all list algorithms use a loop, which is typically a for loop and occasionally a while loop.
True
Which are built in functions or methods for list algorithms?
Sum, Count, Max/Min, Sort
What does the Sum function do to a list?
Adds together all the elements?
What does the Count function do to a list?
Finds the number of occurrences of a value
What does the Max/Min function do to a list?
Finds the largest/smallest value
What does the Sort function do to a list?
Rearranges the elements to be in order
What are characteristics that relate to needing an accumulator with an initial value to use the sum function on a list?
Zero is the additive identity (adding zero doesn't change anything), one is the multiplicative identity, for a string, it's the "" empty string for concatenation, for a list, it's the [] empty list for concatenation or appending
T or F: Variations are built-in functions.
False
The algorithm for the sum function being used on a list is....
1. Initialize the accumulator to the identity factor for the operation involved
2. For each element in the list, add it to the accumulator (or whatever operation is called for)
The algorithm for the count function being used on a list is....
1. Initialize the counter to zero
2. For each element in the list: 1 if it equals the search value, add 1 to the counter
Variation of the count function on a list...
Count the elements with a certain property (>0, even. etc.)
The algorithm for the max/min function being used on a list is....
1. Initialize the "best" variable to the first element of the list
2. For each element in the rest of the list: .1 if its bigger than the best, it's the new best
T or F: To use the man/min function on a list, elements must be comparable (all strings or all numbers, not a mix of types).
True
Variation of the count function on a list...
Location fo the maximum, maximum of a function applied to each element