Function
expression
>>>max(2, 3)
3
>>>min(2, 3)
2
>>>from operator import add, mul
>>>add(2, 3)
5
>>>mul(2, 3)
6
>>>mul(add(2, 9), mul(3, 4))
132
Types of Expressions
- primitive expressions:
- number of numeral: 2
- name: radius
- string: 'hello'
- call expression:
- max (2, 3)
- operator (operand comma operand) parenthesis
name
>>> from math import pi, sin
>>> pi
3.141592653589793
>>> sin (pi / 2)
1
>>> radius = 10
>>> area, circ = pi * radius * radius, 2 * pi * radius
>>> area
314....
>>> radius = 20
>>> area
314....
# things are not sync
# evaluate pi * pi * radius, then the value get bounded to area, but area forget it was defined in terms of radius
# how might we keep them in sync
>>> def area():
... return pi * radius * radius
...
>>> area() # have no operands
1256(omission)
>>> pi * radius * radius
1256
>>> radius = 10
>>> area()
314
# keep them in sync by function
>>> max
<built-in function max>
>>> f = max
>>> f
<built-in function max>
# f and max are both names for the function
>>> max = 7
# broke the origin max function, max means sth different
>>> f(1, 2, max)
7
>>> max = f
# fortunately, i have the name for that
>>> max
<built-in function max>
>>>def square(x):
... return mul(x, x)
...
>>> square
<function square at 0x000001C437536A60>
>>> def sum_squares(x, y):
return square(x) + square(y)
>>> sum_square(3, 4)
25
- import a name
- from operator import add, mul
- assign a value to name
- radius = 10
- def statement by creating our own function
- def area()
Environment Diagrams
Keep track of what names mean
- Each name is bound to at most one value in a frame
- Within a frame, a name cannot be repeated
pythontutor visualize code, making it clearer
https://pythontutor.com/visualize.html#mode=edit
How to draw an expression tree
- evaluate operator
- evaluate operands
- evaluate subexpression
Function
sorts:
- built-in function
- user-defined function
from operator import mul
def square(x):
return mul(x, x)
square(-2)
square(x): function signature
x: formal parameter
-2: argument value
- global frames
- local frames
an environment is a sequence of frames
a name evaluates the value bound to the name in the earliest frame of the current environment
to look up some names in the function
- look for the name in the local frame
- if not found, look for it in the global frame
use environment more. it's a precise and useful way of keeping track of what happens in the program
environment diagrams only draw user-defined functions
How to write a good function
- func name is short and characteristic
- don't repeat yourself
- functions should be defined generally
documentation
docstring:
- describe the job of function in the first line
- following lines describe the arguments and behavior of the func
use help
to see the docstring
help(func)
x = 12
def f():
'''copy x to a'''
a = x
b = a + 1
print(a, b)
f()
print('hi number ' + str(2))
# str(2) equals to str( 2)
# swap
a, b = b, a
min and max are built-in functions
# when calling a function
# it always evaluate the parameter first, then execute the body.
limit(x, 1/x, a)
# when assignning 0 to x, it causes ZeroDivisionError, because it evaluates 1 / x
HW
>>> c = 'c';print('a');print('b'*2)
# we can use semicolons as delimitors
# use integers to divide, don't use /, use // instead. so that it can generate an integer
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人