CS61A被公认为伯克利的一门神课,主讲语言为python,学透彻编程能力会有质的飞跃(个人观点),课程中的所有lab,hw,pro都可以下载练习,课程体验很棒~博主19年寒假的时候学习了一部分,准备利用暑假时间整理+学习
课程网址:https://cs61a.org/
基本操作
课程用的sublime,轻量级没法直接运行,高亮体验很棒~
写好后进入命令行交互运行 python3 -i lab01.py
运行doctest python3 -m doctest lab01.py
容易忽视的一些知识点
Division
True Division: / (decimal division) | Floor Division // (integer division) |
---|---|
1 / 5 0.2 | 1 // 5 0 |
25 / 4 6.25 | 25 // 4 6 |
4 / 2 2.0不是2 | 4 // 2 2 |
return
and print
def what_prints():
print('Hello World!')
return 'Exiting this function.'
print('61A is awesome!')
>>> what_prints()
Hello World!
'Exiting this function.'
print 打印出的字符串没有引号,return 返回的字符串有引号
Boolean Operators
not
has the higest priorityand
or
has the lowest priority
>>> True and not False or not True and False
>>> (True and (not False)) or ((not True) and False)
python的False:Falae
[]
{}
()
0
None
etc.
boolean operators 返回值
不必一定返回False,True,可以返回其他值
- and stops evaluating any more expressions (short-circuits) once it reaches the
first false value and returns it. If all values evaluate to a true value, the last
value is returned. - or short-circuits at the first true value and returns it. If all values evaluate to
a false value, the last value is returned.
>>> True and 1 / 0 and False
Error (ZeroDivisionError)
>>> True or 1 / 0 or False
True
>>> True and 0
0
>>> False or 1
1
>>> 1 and 3 and 6 and 10 and 15
15
>>> 0 or False or 2 or 1 / 0
2
Call expressions
To evaluate a function call:
- Evaluate the operator, and then the operands (from left to right).
- Apply the operator to the operands (the values of the operands).
If an operand is a nested call expression, then these two steps are applied to that inner operand first in order to evaluate the outer operand.
通俗来讲就是先进行括号里的操作,然后apply opperator to opperands,举个例子
print(print(1),print(2))
1
2
None None
补充知识:print函数的返回值为None
先执行print(1) 打印1,换行,然后执行print(2),打印2,然后相当于print(None,None)
Environments Diagrams
变量赋值:变量名 + 值
定义函数:函数名 + 箭头指向函数对象
调用函数
- Evaluate the operator, which should evaluate to a function.
- Evaluate the operands from left to right.
- Draw a new frame, labelling it with the following: 2
• A unique index (f1, f2, f3, …)
• The intrinsic name of the function, which is the name of the function object itself. For example, if the function object is func square(x)
[parent=Global], the intrinsic name is square.
• The parent frame ([parent=Global]) - Bind the formal parameters to the argument values obtained in step 2 (e.g.
bind x to 3). - Evaluate the body of the function in this new frame until a return value is
obtained. Write down the return value in the frame.
python tutor 理解 Environment Diagrams
https://goo.gl/jM37t7
https://goo.gl/mjfnMn
第一篇博客,只是记录了一下所学内容,慢慢来~