30
Q and A for Sections 2.6 – 2.8 CS 106 Victor Norman

Q and A for Sections 2.6 – 2.8

Embed Size (px)

DESCRIPTION

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? - PowerPoint PPT Presentation

Citation preview

Page 1: Q and A for Sections 2.6 – 2.8

Q and A for Sections 2.6 – 2.8

CS 106Victor Norman

Page 2: Q and A for Sections 2.6 – 2.8

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…

Page 3: Q and A for Sections 2.6 – 2.8

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.

Page 4: Q and A for Sections 2.6 – 2.8

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

Page 5: Q and A for Sections 2.6 – 2.8

More examples of pure functions

• look at page 62

Q: Is the converting letters to numbers function (ord()) commonly useful?A: No.

Page 6: Q and A for Sections 2.6 – 2.8

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.

Page 7: Q and A for Sections 2.6 – 2.8

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 *

Page 8: Q and A for Sections 2.6 – 2.8

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

Page 9: Q and A for Sections 2.6 – 2.8

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

Page 10: Q and A for Sections 2.6 – 2.8

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’

Page 11: Q and A for Sections 2.6 – 2.8

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.

Page 12: Q and A for Sections 2.6 – 2.8

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 = Falsefor female in listOfFemales: if female.hairColor == “brown”: found = True possibleWife = femaleif found: askOnDate(possibleWife)

Page 13: Q and A for Sections 2.6 – 2.8

Figure 2.12

Q: Do we have to memorize Figure 2.12?

A: No. In your gut you know this table anyway.

Page 14: Q and A for Sections 2.6 – 2.8

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?

Page 15: Q and A for Sections 2.6 – 2.8

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 != .

Page 16: Q and A for Sections 2.6 – 2.8

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.

Page 17: Q and A for Sections 2.6 – 2.8

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)

Page 18: Q and A for Sections 2.6 – 2.8

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

Page 19: Q and A for Sections 2.6 – 2.8

Extra old slides

Page 20: Q and A for Sections 2.6 – 2.8
Page 21: Q and A for Sections 2.6 – 2.8

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.

Page 22: Q and A for Sections 2.6 – 2.8

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

Page 23: Q and A for Sections 2.6 – 2.8

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)

Page 24: Q and A for Sections 2.6 – 2.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.

Page 25: Q and A for Sections 2.6 – 2.8

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

Page 26: Q and A for Sections 2.6 – 2.8

Is True true?

Q: Shorten this code:if is_cheap == True: buy_it()

A: if is_cheap: buy_it()

Page 27: Q and A for Sections 2.6 – 2.8

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

Page 28: Q and A for Sections 2.6 – 2.8

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)

Page 29: Q and A for Sections 2.6 – 2.8

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'

Page 30: Q and A for Sections 2.6 – 2.8

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]