0
推荐顺序:Textbook -> Video -> Lecture's full -> lab and homework
Lecture 1 Intro
Lab 00
最后加上--local
,例如:python3 ok -q python-basics -u --local
,之后所有的测试都加上--local
。
Lecture 2 Function
video
https://pythontutor.com/cp/composingprograms.html#mode=edit
Environment Diagrams: 我理解的就是类似于当前所有元素(包括变量,函数,对象等)的集合。
Frame: 运行过程中将name与expression绑定(bound)起来。每一次函数调用都会有一个新的Frame。
HW 01
主要就是这个Q5做的时候一直没看懂是什么意思。经过一番折腾,好在是过了。
要点:
根据函数
with_if_statement
和with_if_function
的返回值都是None
得知,true_func
和false_func
均无返回值。调用
with_if_statement
和with_if_function
时打印出来的东西,也就是说明这两个函数的函数体内有print
。当函数做函数参数时,无论什么情况都会调用。例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16def func(condition, true, false):
if (condition):
return true
else:
return false
def condition():
return True
def true():
print('true')
def false():
print('false')
func(condition(), true(), false())打印结果:
1
2true
false注:调用
func
函数时,传的参数是有括号的,即调用时函数参数condition
,true
以及false
都是函数condition()
,true()
以及false()
的返回值,这样无论condition
是否为真都需要调用true()
和false()
也就不奇怪了。补充:调用
func
函数时,传的参数没有括号,无任何打印。
1 | # Q2 |
Lecture 3 Control
video
python3 -i xx.py
introduce condition statement and iteration.
Lab 01
非0
即真,非False
即真,非None
即真。
Q3: Debugging Quiz! 没好好做,貌似都是关于debug的。
1 | # Q4 |
Lecture 4 & 5
Textbook 1.6 Higher-Order Functions
1.6.1 Functions as Arguments
其实就是将函数作为参数:
1 | def sum(n, term): |
1.6.2 Functions as General Methods
迭代计算黄金分割率:
1 | """ |
1.6.3 Defining Functions III: Nested Definitions
1 | """ |
1.6.6 Currying
1 | """ |
1.6.8 Abstractions and First-Class Functions
A programming language is said to have First-class functions when functions in that language are treated like any other variable.
1.6.9 Function Decorators
1 | def trace1(func): |
lab 02
1 | # Q1 |
Lecture 6 & 7
pass
Lecture 8 & 9
HW 02
1 | # Q1 |
Lecture 10 & 11
Textbook 2.2 Data Abstraction
2.2.1 & 2.2.2
由于浮点数有精度损失的问题,例如:1 / 3 == 0.333333333333333300000 is True
,因此希望有某种方式来表示有理数,这里采用一个分母,一个分子来表示无理数,则上面的例子为:等号左边为:分子1,分母3,等号右边为:分子0.333333333333333300000,分母1。根据Chapter
1中将一系列运算抽象成函数的方法,我们首先假设已经实现了两个操作:1.
可以使用分子分母初始化一个有理数。2.
获取有理数的分子或者分母。这种假设很有用,可以暂时屏蔽底层的实现,更加关注运算层面的实现。
2.2.3 Abstraction Barriers
实现有理数的抽象过程中,我们可以分为三个大的部分:
- 有理数的运算:
add_rational, mul_rational, print_rational
等等,即对有理数的操作。 - 初始化有理数:
rational, numer, denom
。 - 有理数如何去存储。
可以作为分层的参考。
不好的抽象方式:
add_rational([1, 2], [1, 4])
,将1和2结合起来。def add_rational(x, y): return [x[0] * y[1] + x[1] * y[0], x[1] * y[1]]
,将2和3结合起来。
Lab 04
1 | # Q2 |
Lecture 12 Trees
Textbook 2.3
讲的都是Sequence的语法,没仔细看。
Lecture 13 Binary Numbers (optional)
Lab 05
1 | # Q1 |
Lecture 14 Circuits
pass
Lecture 15 & 16 Mutable Values & Functions
Textbook 2.4
Adding state to data is a central ingredient of a paradigm called object-oriented programming.
Dictionaries: Tuples are commonly used for keys in dictionaries because lists cannot be used. ??
nonlocal: 我的理解就是,内部的函数可以使用外部的变量(即赋值的右边),但是不可以修改外部的变量,若需要修改则需要加上nonlocal关键字,来表明这个变量是外部的变量。


nonlocal的好处就是,可以维持内部的状态。貌似就是类中的属性,通过方法去修改它的值。
HW 03
1 | # Q1 |
Lab 06
1 | # Q1 |
Lecture 17 & 18 Iterators & Objects
HW 04
1 | # Q1 |