Q and A for Sections 2.6 – 2.8 CS 106 Victor Norman Pure Functions Q: Why are there pure functions? Why not just normal functions? A: You may call them “normal” functions, if you want. They really are just normal functions: they take parameters and compute and return a value. Q: What makes a function “pure”? A: It only thinks clean thoughts… 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 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. min() and max() Q: Can you call min() or max() of a list that isn’t integers or floats? A: Yes! As long as the items in the list can be compared to each other, there is a min() and a max() value. 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 math from 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 Huh? Student writes, ‘that eerie sense that “I’ve experienced this before.” Cues from the current situation may unconsciously trigger retrieval of an earlier experience’ 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. 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. found = False for female in listOfFemales: if female.hairColor == “brown”: found = True possibleWife = female if found: askOnDate(possibleWife) Figure 2.12 Q: Do we have to memorize Figure 2.12? A: No. In your gut you know this table anyway. Practice with boolean expressions • How do you decide which class to sign up for? How do you decide which section to sign up for? • How do you decide whether or not to have another slice of pizza? • How do you decide what to eat in the dining hall? • How do you decide when to go to sleep? • How do you decide when to ask someone on a date? 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 that 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) • Now, make a variable inMiddle that is the opposite of inCorner. 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) Even More Write a line to set variable is5 to True if the value in num is 5. Write a line to set variable is56or7 to True if the value in num is 5 or 6 or 7. Write a line to set variable isMultOf2Or3 to True if the value in num is a multiple of 2 or a multiple of 3. Write a line to set variable iNotMultOf2Or3 Extra old slides 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 len is 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 - 32 multiplier = 5.0 / 9.0 cels = 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 = True has_money = False has_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 = True is_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 were not is_tall and not is_employable then 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]