Lecture 02 - CS61A fa2022
To-do
- How to publish slides?
"Zoom" rules - Learning English Time
Expressions and Values
Expressions: 'a' + 'hoy'
Value: 'ahoy'
Expressions
- Two ways and combination:
1+2(expression with operation)pow(2,3)(call expression) pow()is built-in, while others (likeadd()) must be imported byfrom operator import add. More opreators.- Why need
add()?
- Why need
-
How Python evaluates a call expression:
Evaluate the operator Evaluate the operands Apply the operator (a function) to the evaluated operands (arguments)
Operators and operands are also expressions, so they must be evaluated to discover their values.
- What does it mean? functions and argument are not values?
Answer: See the following example.
- What does it mean? functions and argument are not values?
-
Nested Expressions: Expression Tree
- Recursively or iteratively?
- Name: just the name of the var.
Binding a name to a value: (English!)var_a(name)=(assigned by)1(value)var_a(name)=(assigned by)1 + 1(expression)
- Variable: A name that is bound to a value.
- More of operators.
Environment Diagrams
Just a GUI simple debuger.
Functions
- function, args and return value
def- Syntax:
def <name>(<parameters>): # ← Function signature return <return expression> # ← Function body -
A def statement creates a function object with certain parameters and a body and binds it to a name in the current environment.
- Syntax:
- Function is Object, which is very wierd for me with only little background of C.See more here.
Example:f = min f = max g = min h = max max = g max(f(2, g(h(1, 5), 3)), 4) # - Note the parameters and return values of functions.
- Calling a function is creating a stack frame.
- Environment: Assembly consisting of all frames of stacks of a specific scope.
- Name lookup rules: local -> global ->
NameError
Terms
| en | zh_CN | Supplement | Example |
|---|---|---|---|
| Anatomy of a Call Expression | 调用表达式剖析 | call expressions: e.g. pow(10,3) = 1000 anatomy: 解剖学 |
|
| evaluate expressions | 对表达式求值 | The Python interpreter evaluates expressions and displays their values. | |
| in this sequence | in this block of codes | ||
| environment diagrams | 环境图 | a visualization of how Python interprets a program |
Vocabs and Phrases
| en | Explanation | Example |
|---|---|---|
| tangential | 切向的;合...的 | If you have unanswered or tangential questions, post in the Piazza Q&A thread. |