cs61a pp+ch. 1.1-1.2+HW01

这篇博客涵盖了CS61A课程的初步内容,包括表达式和函数的概念,Python之禅,URL访问,以及作业HW01的解答。讲解了函数调用、环境框架、局部与全局变量、赋值语句的执行规则,并介绍了SICP、Elements of Programming等编程思想。还探讨了纯函数的优点和调试技巧,并提供了几个编程问题的解题提示和答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)到右边值

定义一个函数

自定义函数的执行过程:

  1. Add a local frame, forming a new environment

  2. Bind the function’s formal parameters to its arguments in that frame

  3. 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值