1 / 14

Q and A for Sections 2.6 – 2.8

Q and A for Sections 2.6 – 2.8. CS 106 Victor Norman. Pure Functions. Q: If the syntax for a method call is object.method (parameters) , then what is the syntax for a pure function call ? A: function(parameters ) Q: What is a non-pure function called? An “impure” function?

chuong
Download Presentation

Q and A for Sections 2.6 – 2.8

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.6 – 2.8 CS 106 Victor Norman

  2. Pure Functions Q: If the syntax for a method call is object.method(parameters), then what is the syntax for a pure function call? A: function(parameters) Q: What is a non-pure function called? An “impure” function? A: It is called a bound function, because it belongs to an object.

  3. Using common pure functions Q: Suppose you are given a list, and you want to iterate through the list using an index. I.e., you want to get a list of integers 0, 1, 2, 3, ..., len-1, where lenis the number of items in the list. Use range() and len() to do this, on list majors. A: range(len(majors)) (Note to student: memorize this. You'll see it a lot.)

  4. imports Q: Suppose we want to use the value for pi in our code. The book says there are 3 ways to import code from a library. Here are 2 ways. What is the 3rd?import mathfrom math import pi ________________ A: from math import *

  5. Qualified names Q: Which of the 3 ways of importing a library requires you to use qualified names? A: The first. E.g.,import math (BTW, this is the preferred way, IMO. Why? So we don't "pollute" the namespace.)

  6. What’s in a library? Q: How does a young enterprising student find out what libraries are available and what's in these libraries? A: google

  7. Compound expressions Q: Rewrite this code as a single compound expression, on one line:tmp1 = fehr - 32multiplier = 5.0 / 9.0cels = multiplier * tmp1 A: cels = (5.0 / 9.0) * (fehr - 32)

  8. Assignment details Q: Why does code like this work?num_seen = num_seen + 1 A: It works because = is an operator and the rules say that you evaluate each side and then apply = (assignment) last. So, num_seen + 1 is evaluated to some value, and then num_seen is set to refer to that value.

  9. Evaluating boolean expressions Q: What does this evaluate to, True or False?:is_cute = Truehas_money = Falsehas_a_car = True (is_cute and not has_money and has_a_car) or has_money A: True: (T and T and T) or F

  10. Is True true? Q: Shorten this code:if is_cheap == True:buy_it() A: if is_cheap:buy_it()

  11. Eval Boolean Expressions (2) Q: What does the last line below evaluate to?is_dutch = Trueis_much = <this value hidden from view>is_dutch or is_much A: True

  12. Eval Boolean Expressions (3) Q: Are these equivalent?:not (is_tall or is_employable)not is_tall or not is_employable A: No: if the second one werenot is_tall and not is_employablethen they would be equivalent(this is something called DeMorgan's Law)

  13. Eval Boolean Expressions (4) Q: What does this do?:if len(names_list) > 20 and \ names_list[-1].startsWith(’Glaza'):do_something() A: do_something() is called if there are more than 20 names in the list and the last name in the list starts with the string ’Glaza'

  14. Hint for upcoming assignment… Q: Picking off a word from within a line is a common task. Rewrite this code to use one line:words = line.split()third_word = words[2] A: third_word = line.split()[2]

More Related