190111- Dartmouth COCS1 【第五章:参数、返回值和范围】

失踪人口回归,没想到之前的居然真的有人看,有点开心~ 现在考试和申请都基本结束了,好好继续学习!

今天进度比较少,只看了【第五章:参数、返回值和范围】,还是只写新学和觉得需要注意的,基础用法就不提了

列了一些常用的编程相关的英文,看还是都看得懂的,有时候要说不一定能说得出专业词汇,所以做一个小记录,等整个教程看完,做一个词汇的专门整理。

program 程序

function 函数

variable 变量

value 值

function call 函数调用

function header 函数头

comma 逗号

parameter 参数

formal parmeter 形参

actual parmeter / argument 实参

return value 返回值

pass 传递

operand 操作符

compute 计算

execute 操作

type 类型

convert 转换 from... to ...

be truncated(dropped) 被截去

store 存储

decimal point 小数点

module 模块

statement 声明

editor 编辑器

program counter 程序计数器

return 返回

dead code 不会被运行的代码

substitute 替代

expression 表达式

local variable 局部变量

global variable 全局变量

disposable 一次性的

be local to 对...是局部的

be accessed (值)被获取

assign 分配

frame 框架

evaluate 评估

 

局部变量可以即用即销毁,不占用太多空间;而全局变量改变时需特别注意,所以python会要求,在函数内要改变全局变量,需先用global声明,但如果只是要使用全局变量的值,而不做改变的话,可以不用声明

Function frames and scope

The values of variables are stored in memory. Where? Each function call creates an area of memory called a frame to store values of the local variables that it creates. When the function is exited, the frame is destroyed, and the local variables are no longer in scope. This is good: the function is cleaning up after itself.

Changing the value of a global variable is a big deal. Why? Doing so may affect countless other functions, some of which you might not even know about or have written. Where possible, you should avoid changing global variables, and Python forces you to explicitly tell it that the variable is global before using any assignment statements.

全局变量总的来说还是要谨慎使用,但有一个安全的用途,就是作为名字常量(name constant),全用大写表示,作为各种常用数字的代表,如PI

下边这一段关于python中pi的描述太有意思了,Devin大佬这飘忽不定的幽默感啊。总之,别瞎改默认变量!

Notice that pi is not capitalized in this case. The capitalization is just a convention, and apparently the Python designers didn’t use that convention. Bad Guido! (That’s Guido van Rossum, the designer of Python, who is also known as The Benevolent Dictator For Life.)

Although a global variable such as pi imported from the math library is intended to be constant, Python doesn’t prevent you from changing it.

(I checked on the Internet about this story that the Great State of Kansas attempted to change the value of π to 3. Turns out it’s not true. Yay, Kansas!)

With great power comes great responsibility. Python lets you change the value of variables intended to serve as named constants. Don’t do it.

 最后一个!以前一直没注意过这个问题:简化布尔值表达式相关的代码。对比以下两段代码,输出其实是完全一样的,但明显第二段简化了很多,也才是一个程序员应有的写法,当要输出布尔值时都需要注意到这个问题。

def is_five(x):
    if x == 5:
        return True
    else:
        return False

print( is_five(4) )
print( is_five(5) )
def is_five(x):
    return x == 5

print( is_five(4) )
print( is_five(5) )

不然真就是:(手动狗头)

you are declaring to all the world “I don’t understand the boolean type!!!”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值