Intro To Computing - Python 3 Pt 2
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[5])
3.14 is printed because it 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 = alist.pop(0) print(alist)
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 = [3, 67, "cat", 3.14, False] print(len(alist))
5
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(len(alist))
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 = [ [4, [True, False], 6, 8], [888, 999] ] if alist[0][1][0]: print(alist[1][0]) else: print(alist[1][1])
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())
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)
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 = [4, 2, 8, 6, 5] alist = alist + 999 print(alist)
Error, you cannot concatenate a list with an integer. In order to perform concatenation you would need to write alist+[999]. You must have two lists.
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(57 in alist)
False. 57 is not a top level item in alist. It is in a sublist.
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(3.14 in alist)
True
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[4:])
[ [ ], 3.14, False]
What is printed by the following statements? alist = [1, 3, 5] print(alist * 3)
[1, 3, 5, 1, 3, 5, 1, 3, 5]
What is printed by the following statements? alist = [1, 3, 5] blist = [2, 4, 6] print(alist + blist)
[1, 3, 5, 2, 4, 6]
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)
[10]
What is printed by the following statements? alist = [4, 2, 8, 6, 5] temp = alist.pop(2) temp = alist.pop() print(alist)
[4, 2, 6]
What is printed by the following statements? alist = [4, 2, 8, 6, 5] alist.append(True) alist.append(False) print(alist)
[4, 2, 8, 6, 5, True, False]
What is printed by the following statements? alist = [4, 2, 8, 6, 5] blist = alist * 2 blist[3] = 999 print(alist)
[4, 2, 8, 6, 5] alist was unchanged by the assignment statement. blist was a copy of the references in alist.
What is printed by the following statements? alist = [4, 2, 8, 6, 5] blist = alist blist[3] = 999 print(alist)
[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] alist[2] = True print(alist)
[4, 2, True, 6, 5]
What is printed by the following statements? alist = [4, 2, 8, 6, 5] blist = [ ] for item in alist: blist.append(item+5) print(blist)
[9, 7, 13, 11, 10]. The for loop processes each item of the list. 5 is added before it is appended to blist.
What is printed by the following statements? alist = [4, 2, 8, 6, 5] alist.insert(2, True) alist.insert(0, False) print(alist)
[False, 4, 2, True, 8, 6, 5]
What is printed by the following statements? alist = [4, 2, 8, 6, 5] blist = [alist] * 2 alist[3] = 999 print(blist)
[[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])
c is printed because the first character of the string at index 2 is c