140 likes | 216 Views
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?
E N D
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? A: It is called a bound function, because it belongs to an object.
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.)
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 *
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.)
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
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)
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.
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
Is True true? Q: Shorten this code:if is_cheap == True:buy_it() A: if is_cheap:buy_it()
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
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)
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'
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]