1/11
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
list
An ordered collection of items or values. For example, [value1, value2, value3, …] describes a list where value1 is the first element, value 2 is the second element, value 3 is the third element, and so on.
array
An ordered collection of elements of the same type.
literal
A literal is a fixed value in source code, such as 5 or "hello".
list element
An individual value in a list that is assigned a unique index.
loop variable
In each iteration step, a variable set to a value in a sequence or other data collection.
lists concept
We use these to group data types with a construct known as a list.
Some other programming languages refer to a list as an array.
ANYTHING CAN BE GROUPED TOGETHER IN A LIST
Numbers, words, fruits, animals, colors.
How college board uses the following to represent assigning values to a list
aList ← [value1, value2, value3...]
This creates a new list that contains the values value1, value2, value3, and … at indices 1, 2, 3, and … respectively and assigns it to aList.
aList ← []
Creates a new empty list and assigns it to aList.
You can also copy a list to another list.
aList ← bList
This assigns a copy of the list bList to the list aList. For example, if bList contains [20, 40, 60], then aList will also contain [20, 40, 60] after the assignment.
how do you add an e;ement to the end of a list?
You can do this using the append method.
The College Board uses APPEND(aList, value) to represent adding items to a list.
how do you get the last item that was placed into the list and remove it, much like you would grab the top item out of a basket or bag?
You use the pop method
It removes the last element of the list and assigns it to a variable.
how do you repeat a task for each item in the list?
By using a for loop.
A for loop retrieves each element from the list and assigns it to the loop variable.
Using the loop variable, you can have the program execute the same set of statements for each element. In effect, the loop variable can be assigned to each element of a list on each interaction of the list.
The College Board uses the following notation to represent a for loop:
FOR EACH item IN aList
{
<block of statements>
}
Assume you have a list of numbers such as a_list= [1,2,3,4,5]
Which of the following statements will add a new number to the list?
a_list=a_list +6
a_list=(6)
a_list.append(6)
a_list.add(6)
C
Assume that you have a list of numbers such as a_list=[1,2,3,4,5]
Select the for statement that iterates through the list.
For num in a_list:
For (num,a_list):
For each (num in a_list):
For each (num,a_list):
A
Assume that you have a list of numbers such as a_list= [1,2,3,4,5]
What would the value of num be after the following code?
num=a_list.pop()
num=a_list.pop()
1
2
4
5
C