Chapter 10, Cs133p
What is printed by the following statements? alist = [3, 67, "cat", 3.14, False] print(len(alist)) (A) 4 (B) 5
(B) 5 there are 5 items in this list.
What is printed by the following statements? alist = [4,2,8,6,5] blist = [num*2 for num in alist if num%2==1] print(blist) (A) [4,2,8,6,5] (B) [8,4,16,12,10] (C) 10 (D) [10].
(D) [10]. 5 is the only odd number in alist. It is doubled before being placed in blist.
lists are mutable
This means we can change an item in a list by accessing it directly as part of the assignment statement.
An optional argument called a ----------- can be used to specify which characters to use as word boundaries
delimiter
append is a list method
which adds the argument passed to it to the end of the list.
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(len(alist)) (A) 7 (B) 8
(A) 7 there are 7 items in this list even though two of them happen to also be lists.
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[4:]) (A) [ [ ], 3.14, False] (B) [ [ ], 3.14] (C) [ [56, 57, "dog"], [ ], 3.14, False]
(A) [ [ ], 3.14, False] the slice starts at index 4 and goes up to and including the last item.
What is printed by the following statements? alist = [4, 2, 8, 6, 5] alist.insert(2, True) alist.insert(0, False) print(alist) (A) [False, 4, 2, True, 8, 6, 5] (B) [4, False, True, 2, 8, 6, 5] (C) [False, 2, True, 6, 5]
(A) [False, 4, 2, True, 8, 6, 5] first True was added at index 2, then False was added at index 0.
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[5]) (A) [ ] (B) 3.14 (C) False
(B) 3.14 3.14 is at index 5 since we start counting at 0 and sublists count as one item.
What is printed by the following statements? alist = [4, 2, 8, 6, 5] alist.append(True) alist.append(False) print(alist) (A) [4, 2, 8, 6, 5, False, True] (B) [4, 2, 8, 6, 5, True, False] (C) [True, False, 4, 2, 8, 6, 5]
(B) [4, 2, 8, 6, 5, True, False] each item is added to the end of the list.
What is printed by the following statements? alist = [4, 2, 8, 6, 5] blist = alist blist[3] = 999 print(alist) (A) [4, 2, 8, 6, 5] (B) [4, 2, 8, 999, 5]
(B) [4, 2, 8, 999, 5] since alist and blist both reference the same list, changes to one also change the other.
What is printed by the following statements? alist = [4, 2, 8, 6, 5] blist = [alist] * 2 alist[3] = 999 print(blist) (A) [4, 2, 8, 999, 5, 4, 2, 8, 999, 5] (B) [[4, 2, 8, 999, 5], [4, 2, 8, 999, 5]] (C) [4, 2, 8, 6, 5] (D) [[4, 2, 8, 999, 5], [4, 2, 8, 6, 5]]
(B) [[4, 2, 8, 999, 5], [4, 2, 8, 999, 5]] blist contains two references, both to alist.
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[2][0]) (A) 56 (B) c (C) cat (D) Error, you cannot have two index values unless you are using slicing.
(B) c the first character of the string at index 2 is c
What is printed by the following statements? alist = [4, 2, 8, 6, 5] alist = alist.pop(0) print(alist) (A) [2, 8, 6, 5] (B) [4, 2, 8, 6, 5] (C) 4 (D) None
(C) 4 first the 4 was removed from the list, then returned and assigned to alist. The list is lost.
What is printed by the following statements? alist = [ [4, [True, False], 6, 8], [888, 999] ] if alist[0][1][0]: print(alist[1][0]) else: print(alist[1][1]) (A) 6 (B) 8 (C) 888 (D) 999
(C) 888 alist[0][1][0] is True and alist[1] is the second list, the first item is 888.
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[2].upper()) (A) Error, you cannot use the upper method on a list. (B) 2 (C) CAT
(C) CAT the string cat is upper cased to become CAT.
What is printed by the following statements? myname = "Edgar Allan Poe" namelist = myname.split() init = "" for aname in namelist: init = init + aname[0] print(init) (A) Poe (B) EdgarAllanPoe (C) EAP (D) William Shakespeare
(C) EAP split creates a list of the three names. The for loop iterates through the names and creates a string from the first characters.
What is printed by the following statements? alist = [1, 3, 5] print(alist * 3) (A) 9 (B) [1, 1, 1, 3, 3, 3, 5, 5, 5] (C) [1, 3, 5, 1, 3, 5, 1, 3, 5] (D) [3, 9, 15]
(C) [1, 3, 5, 1, 3, 5, 1, 3, 5] the items of the list are repeated 3 times, one after another.
What is printed by the following statements? alist = [1, 3, 5] blist = [2, 4, 6] print(alist + blist) (A) 6 (B) [1, 2, 3, 4, 5, 6] (C) [1, 3, 5, 2, 4, 6] (D) [3, 7, 11]
(C) [1, 3, 5, 2, 4, 6] a new list with all the items of the first list followed by all those from the second.
What is printed by the following statements? alist = [4, 2, 8, 6, 5] temp = alist.pop(2) temp = alist.pop() print(alist) (A) [4, 8, 6] (B) [2, 6, 5] (C) [4, 2, 6]
(C) [4, 2, 6] first the 8 was removed, then the last item, which was 5.
What is printed by the following statements? alist = [4, 2, 8, 6, 5] blist = alist * 2 blist[3] = 999 print(alist) (A) [4, 2, 8, 999, 5, 4, 2, 8, 6, 5] (B) [4, 2, 8, 999, 5] (C) [4, 2, 8, 6, 5]
(C) [4, 2, 8, 6, 5] alist was unchanged by the assignment statement. blist was a copy of the references in alist.
Finally, there is a special list that contains no elements.
. It is called the empty list and is denoted []
strings are immutable and lists are mutable.
/
A list can contain only integer items. (A) False (B) True
False Yes, unlike strings, lists can consist of any type of Python data.
A list is
a sequential collection of Python data values, where each value is identified by an index.
The append method
adds a new item to the end of a list
Because the same list has two different names, a and b, we say that it is
aliased.
If we want to modify a list and also keep a copy of the original, we need to be able to make a copy of the list itself, not just the reference. This process is sometimes called
cloning The easiest way to clone a list is to use the slice operator.
A pure function
does not produce side effects.
The values that make up a list are called its
elements
An assignment to an element of a list is called
item assignment. Item assignment does not work for strings. Recall that strings are immutable.
A tuple
like a list, is a sequence of items of any type. Unlike lists, however, tuples are immutable.
Functions which take lists as arguments and change them during execution are called
modifiers and the changes they make are called side effects
A list within another list is said to be
nested and the inner list is often called a sublist
The del statement
removes an element from a list by using its position.