A variable is a named value that references or stores a piece of data.
=
来对这段数据的区域进行赋值x = 5
print(x)
5
print(x*2)
10
y = 10
print(y - 2)
8
y = True
print(y)
True
变量命名规则:
_
)开头numberOfRabbits = 40
courseIs15112 = True
99problems = 0 # 会崩溃!因为变量名以数字开头
Cell In [13], line 1 99problems = 0 # 会崩溃!因为变量名以数字开头 ^ SyntaxError: invalid syntax
import keyword
keyword.kwlist
['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']
x = 5
x += 2 # 等价于 x = x + 2
print(x) # 7
7
# 换成其他运算符也是同理
y = 350
y //= 10
print(y) # 35
35
a = b = c = 2
print(f"a={a}, b={b}, c={c}")
a=2, b=2, c=2
a, b, c = 1, 2, 6
print(f"a={a}, b={b}, c={c}")
a=1, b=2, c=6
A function is a procedure (a sequence of statements) stored under a name that can be used repeatedly by calling the name.
header
用于定义函数接口(函数 名称 与 参数)body
包含函数所需要执行的操作header
用于定义函数的名称和参数
body
部分,
分隔),也可以不提供参数(0 个)header
以冒号(:
)结尾,代表后面会跟着 body
部分函数的 header
的写法:
def functionName(parameters):
pass # 函数的 body 部分,这里使用 pass 代替
body
包含函数执行的语句(statement
)
return
语句,来让函数返回其结果,但不是必须的类似于用一个
=
来对多个变量赋值,函数的返回结果也可以不止一个(用逗号,
分隔)
下面我们用一个例子来解释函数的细节
def double(x):
print("我在一个名叫 “double” 函数里!")
return 2 * x
return
语句的值调用示例函数
double()
会返回一个值(2 * x
)
print(double(2)) # 会输出 4
我在一个名叫 “double” 函数里! 4
print(double(5)) # 会输出 10
我在一个名叫 “double” 函数里! 10
print(double(1) + 3) # 会输出 5
我在一个名叫 “double” 函数里! 5
函数可以有任意多个参数,也可以一个都没有
# 三个参数
def f(x, y, z):
return x + y + z
print(f(1, 3, 2)) # 返回 6
6
# 无参数
def g():
return 42
print(g()) # 返回 42
42
可是如果参数数目没有匹配的话……Oops!
print(g(2)) # 崩溃!
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In [29], line 1 ----> 1 print(g(2)) TypeError: g() takes 0 positional arguments but 1 was given
print(f(1, 2)) # 也会崩溃
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In [30], line 1 ----> 1 print(f(1, 2)) TypeError: f() missing 1 required positional argument: 'z'
def Multi_Return_Values():
return 9, 2, 8
a, b, c = Multi_Return_Values()
print(f"a={a}, b={b}, c={c}")
a=9, b=2, c=8
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.
对于语句
表达式的一些例子
4
4
"Hello World"
'Hello World'
7 + 2
9
True or False
True
(2 < 3) and (9 > 0)
True
Python 只能 print 值和表达式,如果你能用 print()
输出它,那它就是表达式
print((2 < 3) and (9 > 0))
True
语句的一些例子
def f(x):
return 5*x
x = 5 + 4
if 10 > 5:
y = 5 + 3
就是 Python 自己带的函数啦🤗
print(bool(0)) # 转换为布尔类型(True or False)
False
print(float(42)) # 转换为浮点数
42.0
print(int(2.8)) # 转换为一个整数(舍弃小数点)
2
但是它们不在 math
库中
print(abs(-5)) # 绝对值
5
print(max(2,3)) # 返回最大值
3
print(min(2,3)) # 返回最小值
2
print(pow(2,10)) # 次方运算,等价于 2**10
1024
print(round(2.354, 2)) # 取最近的一个整数(并不完全是四舍五入,二进制精度丢失)
2.35
我们设定一个函数 f(x)
, 它的内部有 x
和 y
两个变量
记得一定要重启 Jupyter Kernel!
def f(x):
print("x:", x)
y = 5
print("y:", y)
return x + y
print(f(4))
x: 4 y: 5 9
print(x) # crash!
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In [3], line 1 ----> 1 print(x) NameError: name 'x' is not defined
print(y) # crash!
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In [4], line 1 ----> 1 print(y) NameError: name 'y' is not defined
函数内的变量具有局部作用域,它只存在于函数内部,与其他函数中的同名变量无关
def f(x):
print("In f, x =", x)
x += 5
return x
def g(x):
y = f(x*2)
print("In g, x =", x)
z = f(x*3)
print("In g, x =", x)
return y + z
print(g(2))
In f, x = 4 In g, x = 2 In f, x = 6 In g, x = 2 20
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)
def f(x):
print("In f, x =", x)
x += 7
return round(x / 3)
def g(x):
x *= 10
return 2 * f(x)
def h(x):
x += 3
return f(x+4) + g(x)
print(h(f(1)))
In f, x = 1 In f, x = 10 In f, x = 60 50
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%207%0A%20%20%20%20return%20round%28x%20/%203%29%0A%0Adef%20g%28x%29%3A%0A%20%20%20%20x%20*%3D%2010%0A%20%20%20%20return%202%20*%20f%28x%29%0A%0Adef%20h%28x%29%3A%0A%20%20%20%20x%20%2B%3D%203%0A%20%20%20%20return%20f%28x%2B4%29%20%2B%20g%28x%29%0A%0Aprint%28h%28f%281%29%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)
在函数外部定义变量时,变量具有全局作用域,在任何地方都可以使用
我们应该尽量避免使用全局变量,但是在非常少的一些场合你会需要用到它
g = 100
def f(x):
return x + g
print(f(5)) # 105
print(f(6)) # 106
print(g) # 100
105 106 100
from IPython.display import IFrame
IFrame('https://pythontutor.com/render.html#code=g%20%3D%20100%0A%0Adef%20f%28x%29%3A%0A%20%20%20%20return%20x%20%2B%20g%0A%0Aprint%28f%285%29%29%20%23%20105%0Aprint%28f%286%29%29%20%23%20106%0Aprint%28g%29%20%20%20%20%23%20100&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false', width=1300, height=600)
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
106 108 102
from IPython.display import IFrame
IFrame('https://pythontutor.com/render.html#code=g%20%3D%20100%0A%0Adef%20f%28x%29%3A%0A%20%20%20%20global%20g%0A%20%20%20%20g%20%2B%3D%201%0A%20%20%20%20return%20x%20%2B%20g%0A%0Aprint%28f%285%29%29%20%23%20106%0Aprint%28f%286%29%29%20%23%20108%0Aprint%28g%29%20%20%20%20%23%20102&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false', width=1300, height=600)
def isPositive(x):
return (x > 0)
print(isPositive(5)) # True
True
print(isPositive(-5)) # False
False
print(isPositive(0)) # False
False
一旦返回,函数立即结束!
def isPositive(x):
print("Hello!") # 会运行
return (x > 0)
print("Goodbye!") # 不会运行
print(isPositive(5)) # 输出 “Hello!” 然后返回 True
Hello! True
from IPython.display import IFrame
IFrame('https://pythontutor.com/render.html#code=def%20isPositive%28x%29%3A%0A%20%20%20%20print%28%22Hello!%22%29%0A%20%20%20%20return%20%28x%20%3E%200%29%0A%20%20%20%20print%28%22Goodbye!%22%29%0A%0Aprint%28isPositive%285%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)
没有返回语句的时候,函数会返回 None
def f(x):
x + 42
print(f(5)) # None
None
def f(x):
result = x + 42
print(f(5)) # None
None
print()
和 return
是初学者比较容易出现的错误
def cubed(x):
print(x**3) # 这里的操作不太合适
cubed(2) # 但是似乎看起来也正常运行了
8
print(cubed(3)) # 应该也能有效(但是返回 None,太怪了)
27 None
print(2*cubed(4)) # Error!
64
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In [41], line 1 ----> 1 print(2*cubed(4)) TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
正确写法:
def cubed(x):
return (x**3) # 这样做更好
cubed(2) #似乎输出被忽略了,为什么?
8
print(cubed(3)) # 有效了!
27
print(2*cubed(4)) # 也是有效的!
128
对于嵌套的函数而言,应该最先运行最内层的函数
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)) # 你一定得“亲眼看看”
500
from IPython.display import IFrame
IFrame('https://pythontutor.com/render.html#code=def%20f%28w%29%3A%0A%20%20%20%20return%2010*w%0A%0Adef%20g%28x,%20y%29%3A%0A%20%20%20%20return%20f%283*x%29%20%2B%20y%0A%0Adef%20h%28z%29%3A%0A%20%20%20%20return%20f%28g%28z,%20f%28z%2B1%29%29%29%0A%0Aprint%28h%281%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)
def onesDigit(n):
return n%10
def largerOnesDigit(x, y):
return max(onesDigit(x), onesDigit(y))
print(largerOnesDigit(134, 672)) # 4
print(largerOnesDigit(132, 674)) # 依然是 4
4 4
from IPython.display import IFrame
IFrame('https://pythontutor.com/render.html#code=def%20onesDigit%28n%29%3A%0A%20%20%20%20return%20n%2510%0A%0Adef%20largerOnesDigit%28x,%20y%29%3A%0A%20%20%20%20return%20max%28onesDigit%28x%29,%20onesDigit%28y%29%29%0A%0Aprint%28largerOnesDigit%28134,%20672%29%29%0Aprint%28largerOnesDigit%28132,%20674%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)
Don’t be the person who “never quite understood” something like recursion.
—— Teach Yourself Computer Science
补充资料:
def
、header、body、缩进、return