
Q1: WWPD: The Truth Will Prevail

The Truth Will Prevail > Suite 1 > Case 1
(cases remaining: 4)
>>>
>>> True and 13
? 13
-- OK! --
(找第一个False value, 但是没有 最终结果是13)
>>> False or 0
? 0
-- OK! --
(找第一个True value, 是0 最终是0)
>>> not 10
? False
-- OK! --
(只有0和None是false 任何非0都是True 所以是False)
>>> not None
? True
-- OK! --
(None是False 所以是True)
---------------------------------------------------------------------
The Truth Will Prevail > Suite 2 > Case 1
(cases remaining: 3)
What would Python display? If you get stuck, try it out in the Python
interpreter!
>>> True and 1 / 0 # If this errors, just type Error.
? Error
-- OK! --
(找第一个False 跳到1/0 Error了)
>>> True or 1 / 0 # If this errors, just type Error.
? True
-- OK! --
(找第一个True 后面被短路了)
>>> -1 and 1 > 0 # If this errors, just type Error.
? True
-- OK! --
(找False,找不到 则为True)
>>> -1 or 5
? -1
-- OK! --
(找第一个True, 非0都为True, 则短路, 为-1)
>>> (1 + 1) and 1 # If this errors, just type Error. If this is blank, just type Nothing.
? 1
-- OK! --
(找False,但都是True,最后处理的是1,则为1)
---------------------------------------------------------------------
The Truth Will Prevail > Suite 2 > Case 2
(cases remaining: 2)
What would Python display? If you get stuck, try it out in the Python
interpreter!
>>> print(3) or ""
(line 1)? 3
(line 2)? ""
-- OK! --
(print(3)直接运行输出3换行, 然后处理or的判断:print完没有返回值,相当于None,为False,因此转到“”输出)
---------------------------------------------------------------------
The Truth Will Prevail > Suite 3 > Case 1
(cases remaining: 1)
What would Python display? If you get stuck, try it out in the Python
interpreter!
>>> def f(x):
... if x == 0:
... return "zero"
... elif x > 0:
... return "positive"
... else:
... return ""
>>> 0 or f(1)
? "positive"
-- OK! --
(0是False,跳到f(1),返回是“positive”)
>>> f(0) or f(-1)
? "zero"
-- OK! --
(f(0)有返回值"zero",判断为True,直接短路,输出)
>>> f(0) and f(-1)
? ""
-- OK! --
(f(0)有返回值"zero",判断为True,跳到f(-1)找False,返回“”,输出最终处理值“”)
Q2: WWPD: Higher-Order Functions


>>> # If Python displays <function...>, type Function, if it errors type Error, if it displays nothing type Nothing
>>> def cake():
... print('beets')
... def pie():
... print('sweets')
... return 'cake'
... return pie
>>> chocolate = cake()
? beets
-- OK! --
(简单的赋值语句,将chocolate变量带入函数cake中,造成的后果是输出beets)
>>> chocolate
? Function
-- OK! --
(高阶函数并没有收到第二个信号(),报错了(正确的见下),因此只会显示<function...>,因此为Function)
>>> chocolate()
(line 1)? sweets
(line 2)? 'cake'
-- OK! --
(这次是正确的高阶函数结果表达,进入了第二层函数pie,先是输出sweets并换行,并返回了值”cake“然后被输出)
>>> more_chocolate, more_cake = chocolate(), cake
? sweets
-- OK! --
(还是简单的赋值语句,进入函数pie中,直接输出sweets)
>>> more_chocolate
? 'cake'
-- OK! --
(赋值more_chocolate为chocolate(),为返回值’cake',而后输出)
>>> # Reminder: cake, more_cake, and chocolate were defined/assigned in the code above!
>>> # It might be helpful to refer to their definitions on the assignment website so you don't have to scroll as much!
>>> def snake(x, y):
... if cake == more_cake:
... return chocolate
... else:
... return x + y
>>> snake(10, 20)
? Function
-- OK! --
(根据前文的赋值,cake和more_cake值是一样的,判断为True,返回chocolate,根据前,chocolate为Function)
>>> snake(10, 20)()
(line 1)? sweets
(line 2)? 'cake'
-- OK! --
(等价于chocolate(),同前)
>>> cake = 'cake'
>>> snake(10, 20)
? 30
-- OK! --
(这次cake和more_cake值就不一样了,因此判断为False,返回的是10+20即为30)
Q3: WWPD: Lambda

Lambda the Free > Suite 1 > Case 1
(cases remaining: 5)
Q: Which of the following statements describes a difference between a def statement and a lambda expression?
Choose the number of the correct choice:
0) A lambda expression does not automatically bind the function that it returns to a name.
1) A lambda expression cannot have more than two parameters.
2) A def statement can only have one line in its body.
3) A lambda expression cannot return another function.
? 0
-- OK! --
1)2)不详细说明
0):lambda即为创建匿名函数,“匿名” 的意思就是它所定义的函数不会自动与某个名称绑定。
3)
---------------------------------------------------------------------
Lambda the Free > Suite 1 > Case 2
(cases remaining: 4)
Q: How many formal parameters does the following lambda expression have?
lambda a, b: c + d
Choose the number of the correct choice:
0) three
1) one
2) Not enough information
3) two
? 3
-- OK! --
formal parameter指的就是冒号之前明确列出的变量,为a,b两个
---------------------------------------------------------------------
Lambda the Free > Suite 1 > Case 3
(cases remaining: 3)
Q: When is the return expression of a lambda expression executed?
Choose the number of the correct choice:
0) When you pass the lambda expression into another function.
1) When the lambda expression is evaluated.
2) When the function returned by the lambda expression is called.
3) When you assign the lambda expression to a name.
? 2
-- OK! --
(lambda 表达式本质上是一个匿名函数,当这个函数被调用时,其返回表达式才会被执行)
---------------------------------------------------------------------
Lambda the Free > Suite 2 > Case 1
(cases remaining: 2)
What would Python display? If you get stuck, try it out in the Python
interpreter!
>>> # If Python displays <function...>, type Function, if it errors type Error, if it displays nothing type Nothing
>>> lambda x: x # A lambda expression with one parameter x
? Function
-- OK! --
(因为 `lambda x: x` 是一个定义的匿名函数,当直接在解释器中输入它时,解释器会返回该函数对象本身,而不会执行该函数。)
>>> a = lambda x: x # Assigning a lambda function to the name a
>>> a(5)
? 5
-- OK! --
(使用`lambda`定义了一个匿名函数,并将其赋值给变量a,于是执行a(5),这是在调用之前定义的`lambda`函数,并将参数`5`传递给它。根据`lambda`函数的定义,它会直接返回传入的参数,所以当传入`5`时,函数会返回`5`)
>>> (lambda: 3)() # Using a lambda expression as an operator in a call exp.
? 3
-- OK! --
(基本语法为 `lambda 参数: 表达式`
在`(lambda: 3)` 这个 `lambda` 表达式里,它没有参数(参数部分为空),并且其返回值固定为 `3`。
在 Python 里,要调用一个函数,需要在函数名(或者函数对象)后面加上括号 `()`,括号内可以传入参数。对于 `(lambda: 3)()` 来说,`(lambda: 3)` 是一个函数对象,后面紧跟着的 `()` 表示对这个函数进行调用。因为这个 `lambda` 函数没有参数,所以调用时括号内为空。
由于这个 `lambda` 函数的返回值固定为 `3`,所以当调用 `(lambda: 3)()` 时,Python 会执行该函数并返回 `3`。)
>>> b = lambda x, y: lambda: x + y # Lambdas can return other lambdas!
>>> c = b(8, 4)
>>> c
? Function
-- OK! --
(变量 `c` 指向的是这个内层的 `lambda` 函数,由于 `c` 是一个函数对象,显示Function)
>>> c()
? 12
-- OK! --
(`b = lambda x, y: lambda: x + y` 定义了一个外层 `lambda` 函数 `b`,它接受两个参数 `x` 和 `y`,并返回一个内层的 `lambda` 函数。
当执行 `c = b(8, 4)` 时,调用了外层 `lambda` 函数 `b`,传入参数 `x = 8` 和 `y = 4`,此时外层 `lambda` 函数返回内层的 `lambda` 函数,为`x+y`=12)
>>> d = lambda f: f(4) # They can have functions as arguments as well
>>> def square(x):
... return x * x
>>> d(square)
? 16
-- OK! --
(当执行`d(square)`时,将`square`函数作为参数传递给`d`所指向的`lambda`函数。
此时,`lambda`函数中的`f`就代表`square`函数。然后`lambda`函数会执行`f(4)`,也就是调用`square(4)` = 16)
---------------------------------------------------------------------
Lambda the Free > Suite 2 > Case 2
(cases remaining: 1)
What would Python display? If you get stuck, try it out in the Python
interpreter!
>>> # Try drawing an environment diagram if you get stuck!
>>> higher_order_lambda = lambda f: lambda x: f(x)
>>> g = lambda x: x * x
>>> higher_order_lambda(2)(g) # Which argument belongs to which function call?
? Error
-- OK! --
(当执行 `higher_order_lambda(2)` 时,把整数 `2` 作为参数 `f` 传递给 `higher_order_lambda`。
但 `higher_order_lambda` 期望传入的 `f` 是一个可调用的函数对象,而 `2` 是一个整数,不是可调用对象。
所以当后续尝试调用返回的内层 `lambda` 函数(也就是 `higher_order_lambda(2)` 返回的那个)时,会在执行 `f(x)` 这一步出错,因为 `2` 不能像函数一样被调用。
所以输出Error)
>>> higher_order_lambda(g)(2)
? 4
-- OK! --
(`higher_order_lambda(g)`返回的是`lambda x: g(x)`,接着执行`higher_order_lambda(g)(2)`,相当于调用这个返回的`lambda`函数并传入参数`2`,即执行`g(2)`
即4)
>>> call_thrice = lambda f: lambda x: f(f(f(x)))
>>> call_thrice(lambda y: y + 1)(0)
? 3
-- OK! --
(将`lambda y: y + 1`作为参数传递给`call_thrice`。这个`lambda`函数的作用是将输入的值加`1`。
此时`call_thrice`中的`f`就被绑定为`lambda y: y + 1`,并返回一个新的`lambda`函数`lambda x: (lambda y: y + 1)((lambda y: y + 1)((lambda y: y + 1)(x)))`
经过三次调用,累加了三次1,为3)
>>> print_lambda = lambda z: print(z) # When is the return expression of a lambda expression executed?
>>> print_lambda
? Function
-- OK! --
(同上,为Function)
>>> one_thousand = print_lambda(1000)
? 1000
-- OK! --
(执行`print_lambda(1000)`时,将整数`1000`作为参数传递给`print_lambda`所指向的`lambda`函数。
根据该`lambda`函数的定义,会执行`print(1000)`,这会在控制台输出`1000`)
//同时one_thousand值则是print以后的返回值,为None
>>> one_thousand # What did the call to print_lambda return? If it displays nothing, write Nothing
? Nothing
-- OK! --
(见上)
4221

被折叠的 条评论
为什么被折叠?



