week 4 exercise
What is printed by the following statements? v = 2.34567 print('{:.1f} {:.2f} {:.7f}'.format(v, v, v))
2.3 2.35 2.3456700
What is printed by the following statements? s = "python rocks" print(s.count("o") + s.count("p"))
3
Which of these are good alternatives to the accumulator variable and iterator variable names for the following prompt? Write code to print out each character of the string my_str on a separate line
Accumulator Variable: no variable needed ; Iterator Variable: char
Which of these are good alternatives to the accumulator variable and iterator variable names for the following prompt? Write code to sum up all of the numbers in the list seat_counts. Store that number in the variable total_seat_counts.
Accumulator Variable: total_seats_so_far ; Iterator Variable: seat_count
Which of these are good alternatives to the accumulator variable and iterator variable names for the following prompt? Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels.
Accumulator Variable: total_so_far ; Iterator Variable: letter
Which of these are good alternatives to the accumulator variable and iterator variable names for the following prompt? For each string in wrds, add 'ed' to the end of the word (to make the word past tense). Save these past tense words to a list called past_wrds.
Accumulator Variable: wrds_so_far ; Iterator Variable: wrd
What is printed by the following statements: s = "Ball" s[0] = "C" print(s)
Error
What is printed by the following statements? alist = [4,2,8,6,5] alist = alist + 999 print(alist)
Error; you can not concatenate a list with an integer
What is printed by the following statements: s = "ball" r = "" for item in s: r = item.upper() + r print(r)
LLAB
Does the following prompt require an accumulation pattern? If so, what words indicate that? Write code to print out each character of the string my_str on a separate line.
No
Does the following prompt require an accumulation pattern? If so, what words indicate that? Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels.
Yes,"Code that will count"
Does the following prompt require an accumulation pattern? If so, what words indicate that? Write code to sum up all of the numbers in the list seat_counts. Store that number in the variable total_seat_counts.
Yes,"to sum up"
Does the following prompt require an accumulation pattern? If so, what words indicate that? For each string in wrds, add 'ed' to the end of the word (to make the word past tense). Save these past tense words to a list called past_wrds.
Yes; "save... to a list"
What is the value of y after the following code has been evaluated: w = ['Jamboree', 'get-together', 'party'] y = ['celebration'] y = w
['Jamboree', 'get-together', 'party']
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) print(blist)
[4,2,8,6,5] [4,2,8,999,5,4,2,8,6,5]
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]
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? lst= [3,0,9,4,1,7] new_list=[] for i in range(len(lst)): new_list.append(lst[i]+5) print(new_list)
[8,5,14,9,6,12] the for loop processes each item in lst. 5 is added before lst[i] is appended to blist.
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]
What type should be used for the accumulator variable in the following prompt? Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels.
integer
What type should be used for the accumulator variable in the following prompt? Write code to sum up all of the numbers in the list seat_counts. Store that number in the variable total_seat_counts.
integer
What type should be used for the accumulator variable in the following prompt? For each string in wrds, add 'ed' to the end of the word (to make the word past tense). Save these past tense words to a list called past_wrds.
list
What sequence will you iterate through as you accumulate a result in the following prompt? Write code to print out each character of the string my_str on a separate line.
my_str
What type should be used for the accumulator variable in the following prompt? Write code to print out each character of the string my_str on a separate line.
none, there is no accumulator variable
What sequence will you iterate through as you accumulate a result in the following prompt? Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels.
s
What sequence will you iterate through as you accumulate a result in the following prompt? Write code to sum up all of the numbers in the list seat_counts. Store that number in the variable total_seat_counts.
seat_counts
What is printed by the following statements? x = 2 y = 6 print('sum of {} and {} is {}; product: {}.'.format( x, y, x+y, x*y))
sum of 2 and 6 is 8; product: 12.
What sequence will you iterate through as you accumulate a result in the following prompt? For each string in wrds, add 'ed' to the end of the word (to make the word past tense). Save these past tense words to a list called past_wrds.
wrds
What is printed by the following statements? s = "python rocks" print(s[1]*s.index("n"))
yyyyy s[1] is y and the index of n is 5, so 5 y characters. It is important to realize that the index method has precedence over the repetition operator. Repetition is done last.