250 likes | 349 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 boundfunction or member function, because it belongs to an object.
Pure function? Q: What is this? A pure function or a member function? res = math.sin(0.25) A: It is a pure function, in the math module. Sometimes the "thing" before the . is an object, sometimes it is a module. Sometimes it is really hard to tell the difference. Q: If sin() were a bound function, how would we write it? A: 0.25.sin(), I guess. Which is really weird...
More examples of pure functions • look at page 62 Q: Is the converting letters to numbers function (ord()) commonly useful? A: No.
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. import mathfrom math import pi What is the 3rd? 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
Precedence Q: I understand the concept of precedence but the evaluation trees make it a little confusing. Will we need to be able to draw evaluation trees? A: No.
Does Prof. Norman read our feedback? Q: When will the CS department picnic be? A: Never. We don't have them anymore. (Actually, we do, but we don't invite smart-alecks.)
Boolean • In python, the type is bool. Two possible values: True and False. • We don't often have variables of type bool, but we can. isTallPerson = person.isTall() isBlondePerson = (person.hairColor() == "blonde") # code that uses isTallPerson and isBlondePerson
Figure 2.12 Q: Do we have to memorize Figure 2.12? A: No. In your gut you know this table anyway.
Inclusive vs. exclusive or Q: What is the difference between inclusive 'or' and exclusive 'or'? A: Exclusive or means one is True or the other is True, but not both. It is almost never used. You can use != .
Practice • You have variables x and y which are the location of your ball on the screen, which is size 600 by 400 (600 across, 400 down). Write code to set inCorner to True if the ball is within 20 pixels of the right side and 20 pixels of the bottom (i.e., in the lower-right corner). inCorner = (x >= 580 and y >= 380)
More Write code to set nearEdge to be True if the ball is 20 pixels within the bottom edge or the right edge. nearEdge = (x >= 580 or y >= 380) Now fix it to be True if it is within 20 of any edge. nearEdge = (x < 20 or x >= 580 or y < 20 or y >= 380)
Multiple Functions on 1 line Q: You can call multiple functions on the same line of code? How does that work? A: It is just the same as having values there, but the functions have to be run ("evaluated") to get the value. They are run in the order in which the values are needed, according to precedence runs.
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.)
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]