目录
变量 Variables
新值的数据类型不必与旧值相同
变量是一个标签
变量命名规则:
元组的解包
函数 Functions
`header` 用于定义函数的**名称**和**参数**
`body` 包含函数执行的语句(`statement`)
我们使用**函数名**来调用函数
函数可以有任意多个参数,也可以一个都没有
参数的数量要匹配
语句与表达式 Statements and Expressions
表达式定义:An expression is a data value or an operation that evaluates to a value.
语句定义: Statements, by contrast, do not evaluate to a value, and we can't print them. Usually they perform some action, though.
Python 只能 print 值和表达式,如果你能用 `print()` 输出它,那它就是表达式
内置函数 Builtin Functions:Python 自己带的函数
在Python中,0、空列表[]、空字典{}、空字符串''、None等都被认为是布尔值的False
一旦涉及小数点,就会有精度问题
变量作用域 Variable Scope
函数内的变量具有局部作用域,它只存在于函数内部,与其他函数中的同名变量无关
在函数外部定义变量时,变量具有全局作用域,在任何地方都可以使用
我们应该**尽量避免使用全局变量**,但是在非常少的一些场合你会需要用到它
函数组合 Function Composition
对于嵌套的函数而言,应该最先运行最内层的函数
tip:代码可视化
总结:函数是标签,代码可视化
Helper Functions
每一件工具只用做好一件事情,通过各种工具的组合做很复杂的事情
递归&分治
总结
A variable is a named value that references or stores a piece of data.
- 变量是一个**名字**,它所指代的是一段数据
- 使用 `=` 来对这段数据的区域进行**赋值**
新的值会**覆盖**掉旧的值
0 1 2 3 4
标签是一个地址的标签: 后面那个值所存储的真实的计算机里的物理位置
刘畅 的python
保留字:
['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is','lambda','nonlocal','not','or','pass','raise',
...
'return', 'try', 'while', 'with', 'yield']
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...
多变量赋值:
a, b, c = 1, 2, 6
print(f"a={a}, b={b}, c={c}")
A function is a procedure (a sequence of statements) stored under a name that can be used repeatedly by calling the name.
def functionName(parameters): # f(x,y), define定义
pass # 函数的 body 部分,这里使用 pass 代替
> 类似于用一个 `=` 来对多个变量赋值,函数的返回结果也可以不止一个(用逗号 `,` 分隔)
print(bool(-1.5))
0的boll值是False
0
、空列表[]
、空字典{}
、空字符串''
、None
等都被认为是布尔值的False
基本数学函数(不在math库中):
abs() 绝对值
max() 最大值
min() 最小值
pow() 次方运算
round() 四舍五入
我们设定一个函数 `f(x)`, 它的内部有 `x` 和 `y` 两个变量
记得一定要重启 Jupyter Kernel!
里面的任何修改不影响外面
from IPython.display import IFrame
IFrame('https://pythontutor.com/render.html#code=def%20f%28x%29%3A%0A%20%20%20%20print%28%22In%20f,%20x%20%3D%22,%20x%29%0A%20%20%20%20x%20%2B%3D%205%0A%20%20%20%20return%20x%0A%0Adef%20g%28x%29%3A%0A%20%20%20%20y%20%3D%20f%28x*2%29%0A%20%20%20%20print%28%22In%20g,%20x%20%3D%22,%20x%29%0A%20%20%20%20z%20%3D%20f%28x*3%29%0A%20%20%20%20print%28%22In%20g,%20x%20%3D%22,%20x%29%0A%20%20%20%20return%20y%20%2B%20z%0A%0Aprint%28g%282%29%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false', width=1300, height=600)
区分:x
f::x 或 f.x
g.x
这三个完全不是一回事
g = 100
def f(x):
# 如果我们想要修改 g 的值,我们必须声明它是全局变量
# 否则 Python 会假设它是局部变量
global g
g += 1
return x + g
print(f(5)) # 106
print(f(6)) # 108
print(g) # 102
# 如果我们想要修改 g 的值,我们必须声明它是全局变量
# 否则 Python 会假设它是局部变量
global g
def isPositive(x):
return (x > 0)
一旦return,函数**立即结束!**
没有返回语句的时候,函数会返回 `None`
`print()` 和 `return` 是初学者比较容易出现的错误
print 是显示在屏幕上
return 可以调用的执行结果,才能执行计算
def f(w):
return 10*w
def g(x, y):
return f(3*x) + y #在我们返回它之前,我们必须先执行 f(3*x)
def h(z):
return f(g(z, f(z+1))) # 最内部的 f(z+1) 必须先执行
print(h(1)) # 你一定得“亲眼看看”
编写函数是用来解决问题的
我们还可以编写函数来存储那些经常被用到的一系列操作
这种函数就叫做 `Helper Function`
**Don’t be the person who “never quite understood” something like recursion.**
—— Teach Yourself Computer Science
补充资料:
- [递归&分治](https://oi-wiki.org/basic/divide-and-conquer/)
- [Teach Yourself Computer Science](https://teachyourselfcs.com/)