1 / 23

Q and A for Section 2.9, 4.1

Q and A for Section 2.9, 4.1. Victor Norman CS106 Fall 2014. Interactive vs. Source code modes. Q: Is source code just what we are typing into pyCharm ?

Download Presentation

Q and A for Section 2.9, 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 Section 2.9, 4.1 Victor Norman CS106 Fall 2014

  2. Interactive vs. Source code modes Q: Is source code just what we are typing into pyCharm? A: Yes! When you create a file, and put python in it, you are creating source code. When you hit “Run” in pyCharm, you are actually launching the python interpreter and sending it the file you have been editing. This is “source code” or “non-interactive” or “program” mode. When you just launch python without sending it a file, you are in interactive mode: like a calculator, you can type stuff in, and the interpreter runs it, giving back results for each line.

  3. Properties of Source Code vs. Interactive Modes Source Code Mode • Python interpreter reads code in from a file, and runs it. • Prints out results only when the code says to print something. • Exits when the code is done running. Interactive Mode • Python interpreter started without being given any code to run. • Shows a prompt: >>> • Prints out result after every line is entered by the user. • Does not exit until you type exit() or Ctrl-D. • Very good for checking something quickly. • Nothing you type in is saved for later.

  4. User vs. Programmer • Just hinted at in the book • A programmer needs to think about what the user sees when they run the code • Is it obvious what is happening? • Is the user getting the feedback they need/want? • Can the user customize the output? • Often, you are the programming and the user…

  5. Augmented assignment operators Q: Can you explain how to properly use the += sign? A: What if we could write this in python?: aVal = 7 change aVal by 1 • What would that mean? • Means add 1 to the value in aVal. • aVal += 1 • Equivalent to aVal = aVal + 1 • Similarly for -= *= /=

  6. How print works… • When you call print x, y, z, w + 3, len(guests)these things happen: • Each argument to the print statement is evaluated (converted into a value). • Each value is converted to a str (if it isn’t a string already). • Essentially str(arg) is called on each. • Values are printed out with a space between each. • A newline is outputted, unless the print ends with a comma.

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

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

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

  10. Loop variable declaration/use Q: When the code reads: for person in guests: print personare we in fact assigning the variable name person to every item in guests (in this case a list)? A: Yes! The variable person is defined and then set to iterate through each element of guests. It is just like a variable declaration in an assignment.

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

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

  13. Element-based vs. Index-based loop • A loop is a loop – the loop variable iterates through the items. • An element-based loop is just this: for elem in someList: # do something with each elem in someList.doSomething(elem) • An index-based loop is the same syntax, but a different idea: • Each element in a list is at a certain index. • Access the elements via the index. for idx in range(len(someList)): # sequence is indices now.doSomething(someList[idx]) python tutor example

  14. When do you use which? • Element-based is so easy to read and understand. • Use when you just need each element, and • Don’t care where the element is in the list. • Index-based is more general. • Use when you need to know where the element is in the list. • Use when you need to iterate through multiple lists of the same length. • Use when you need to access elements before or after the current idx.

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

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

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

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

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

  20. Q14 Q: Suppose you are given 2 lists: guys and girls. Write code to print out all possible pairs, like Georg, Gertrude Georg, Helga ... A: for guy in guys: for girl in girls: print guy + ", " + girl

  21. Q15 Q: Suppose you are given 2 lists: guys and girls. Write code to print out all pairs, like Georg, Gertrude Homer, Helga Ichabod, Ingmar A: for i in range(len(guys)): print guys[i] + ", " + girls[i]

  22. Q16 Q: Write code to take a 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)

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

More Related