1 / 18

Q and A for Sections 2.9.1, 4.1

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.

zahina
Download Presentation

Q and A for Sections 2.9.1, 4.1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Q and A for Sections 2.9.1, 4.1 CS 106

  2. Q1 Q: What will the output look like?: print "Hi", "there" print "Hi" + "there" A: Hi there Hithere

  3. Q2 Q: How many arguments are being passed to the print command?: print "This", "is"+ " the day", that, the_Lord + \ has_made A: 4

  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

  5. 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)

  6. 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

  7. 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)

  8. 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.

  9. 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)) )

  10. 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 ...)

  11. 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)

  12. 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.

  13. 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)

  14. 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

  15. 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)

  16. 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]

  17. 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

  18. 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.

More Related