180 likes | 328 Views
Q and A for Sections 2.9.1, 4.1. CS 106. Q1. Q: What will the output look like?: print "Hi", "there" print "Hi" + "there" A: Hi there Hithere. Q2. Q: How many arguments are being passed to the print command?: print "This", "is"+ " the day", that, the_Lord + has_made A: 4. Q3.
E N D
Q1 Q: What will the output look like?: print "Hi", "there" print "Hi" + "there" A: Hi there Hithere
Q2 Q: How many arguments are being passed to the print command?: print "This", "is"+ " the day", that, the_Lord + \ has_made A: 4
Q3 Q: Write the output: print "I am the very model", print "of a modern major general" A: I am the very model of a modern major general
Q4 Q: Write this output: for i in range(10): print i, ", ", print A: 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , (and next output is on a new line)
Q5 Q: What is the problem here, and how do you fix it? i = 42 print "The answer is " + i A: Problem: cannot concatenate a string and int. print "The answer is " + str(i) or print "The answer is", i
Q7 Q: Fill in the blanks: the syntax of a for loop is: for __________ in ___________ : _________ A: for loop-variable in sequence: body (or replace loop-variable with identifier)
Q8 Q: What does this code do, assuming charges is a list of floating point numbers?: total = 0.0 for charge in charges: total += charge print total A: prints the sum of all the values in charges.
Q9 Q: What does this code do?: tot = 0 for i in range(100): tot += i A: calculates the sum of 0 + 1 + 2 + 3 + ... + 99 and stores in tot (could be written as tot = sum(range(100)) )
Q10 Q: Write code that iterates through a list cheeses and prints out i. <the ith item in the list> for each item. A: for i in range(len(cheeses)): print str(i) + ". " + cheeses[i] (or ...)
Q11 Q: What do we call a for loop that uses the index of elements to access elements? A: an index-based loop (p. 131, 132)
Q12 Q: What is different about this for loop: for num in range(1000): val = random.randint(3) do_something(val) A: the loop variable num isn't used. This construct is how we do a loop a certain number of times.
Q13 Q: Suppose you have a sequence (a list or tuple) spanish_inquisition_essentials. Write a loop to call do_something(elem) on each element elem in the list. A: for elem in spanish_inquisition_essentials: do_something(elem)
Q14 Q: Suppose you are given 2 lists: guys and girls. Write code to print out all possible pairs, like George, Gertrude George, Helga ... A: for guy in guys: for girl in girls: print guy + ", " + girl
Q16 Q: Write code to take line of words and produce a string reversed_words that has the same words, each having been reversed. (Note: use a slice to reverse the word.) A: rev_words = [] for word in line.split(): rev_words.append(word[::-1]) reverse_words = " ".join(rev_words)
Q17 Q: Write code to take a list spam_breakfasts and produce a list rev_spam_bfasts which is the same as the original, except with the entries in reverse order. A: rev_spam_bfasts = spam_breakfasts[::-1]
Q18 Q: Write the loop to print out the nth Fibonacci term, assuming n >= 3. A: prev_term = prev_prev_term = 1 for i in range(3, n+1): term = prev_term + prev_prev_term prev_prev_term = prev_term prev_term = term print "nth fib is ", term
Q19 Q: When would you choose an index-based loop and when would you choose an element-based loop? A: You would use an index-based loop when you need to use the index in the calculation (or print it out), or for the ith element, you need to access the i-1th or i+1th element, etc. Or, when you need to iterate through two loops at once. Otherwise, you would use an element-based loop.