pp+ch. 1.1-1.2+HW01
PP
expressions
:An expression describes a computation and evaluates to a value
call expressions
: function call notation (函数调用表达式)
nested expressions
:嵌套表达式
Discussion Question1
f = min
f = max
g, h = min, max
max = g
max(f(2,g(h(1, 5),3)),4) #3
environment diagrams
: Environment diagrams visualize the interpreter’s process([Python Tutor](Python Tutor - Visualize Python, Java, JavaScript, C, C++, Ruby code execution))
分配语句(assignment statements)的执行规则:
- 从
=
右边执行到左边 - 在当前框架下将左边的变量名结合(binding)到右边值
定义一个函数
自定义函数的执行过程:
-
Add a local frame, forming a new environment
-
Bind the function’s formal parameters to its arguments in that frame
-
Execute the body of the function in that new environment
from operator import mul
def square(x):
return mul(x,x)
square(-2)
当前环境要么是全局框架,要么是局部框架+全局框架;一个变量的值是在环境中最近框架里绑定到该名称的值
1.1
Structure and Interpretation of Computer Programs (SICP)
python之禅
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
uniform resource locator (URL)
访问网址
from urllib.request import urlopen
shakespeare = urlopen('http://composingprograms.com/shakespeare.txt') #urlopen()函数获取地址中的文件
words= set(shakespeare.read().decode()