Names
- Values can be assigned to names to make referring to them easier.
- A name can only be bound to a single value.
- Demo One way to introduce a new name in a program is with an assignment statement(赋值语句)
- Statements affect the program, but do not evaluate to values
To execute an assignment statement:
Evaluate
the expression to the right of =.Bind
the value of the expression to the name to the left of = in thecurrent environment
.
Functions
- Functions allow us to abstract away entire expressions and sequences of computation
- They take in some input (known as their arguments) and transform it into an output (the return value)
- We can create functions using def statements. Their input is given in a function call, and their output is given by a return statement.
Drawing Environment Diagrams
- Option 1: Python Tutor (tutor.cs61a.org)
- ○ Useful for quick visualization or for environment diagram questions
- Option 2: PythonAnywhere (editor.pythonanywhere.com)
- ○ Includes an integrated editor/interpreter
- ○ Good for more complicated code or if you want to debug
- ○ Developed by Rahul Arya
Summary
- Programs consist of
statements
, or instructions for the computer, containingexpressions
, which describe computation and evaluate to values. - Values can be assigned to
names
to avoid repeating computations. - An
assignment statement
assigns the value of an expression to a name in the currentenvironment
. Functions
encapsulate a series of statements that mapsarguments
to areturn value
.- A
def statement
creates a function object with certainparameters
and abody
and binds it to a name in the current environment. - A
call expression
applies the value of itsoperator
, a function, to the value(s) or itsoperand
(s), some arguments
Control
None Indicates that Nothing is Returned
- The special value None represents nothing in Python
- A function that does not explicitly return a value will return None
- Careful: None is not displayed by the interpreter as the value of an expression
Pure Functions & Non-Pure Functions(print VS return)
Pure function是指在没有副作用的情况下,根据输入参数返回确定性输出结果的函数。这种函数在不同的时间和不同的上下文中都会返回相同的结果。
- Pure function不改变任何外部状态,也不依赖于外部状态,因此可以被安全地并发执行。
Non-pure function是指在执行过程中会产生副作用(例如改变外部状态、修改全局变量或读取用户输入等)的函数。
- 这种函数的输出结果不仅取决于输入参数,还取决于函数执行时的环境和外部状态。非纯函数难以测试、难以调试,也不便于构建可靠的、可维护的软件系统。
- 纯函数:仅输出返回值
- 非纯函数:有其他结果,如 print(输出(显示)值,并返回 none)
>>> print(print(1), print(2))
1
2
None None
Condition statement
Boolean context is any place where an expression is evaluated to check if it’s a True value or a False value
- False values in Python: False, None, 0, ‘’ (more to come)
- True values: everything else
Boolean Expressions
- Boolean expressions contain special operators and, or, not
Iteration
斐波那契数列
Summary
- print vs None
- None represents when an expressions doesn’t evaluate to a value
- print displays values in the interpreter
- Control
- Allow for the interpreter to selectively or repetitively execute parts of a program
- Iteration
- A particular variant of control which is based in the idea of repeating operations to compute a valu
Comments NOTHING