Lecture 02 - CS61A fa2022

To-do

  • How to publish slides?

"Zoom" rules - Learning English Time

Expressions and Values

Expressions: 'a' + 'hoy'
Value: 'ahoy'

Expressions

  1. Two ways and combination: 1+2(expression with operation) pow(2,3) (call expression)
  2. pow() is built-in, while others (like add()) must be imported by from operator import add. More opreators.
    • Why need add()?
  3. 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.

  4. Nested Expressions: Expression Tree

    • Recursively or iteratively?
  5. Name: just the name of the var.
    Binding a name to a value: (English!)
    1. var_a(name) =(assigned by) 1(value)
    2. var_a(name) =(assigned by) 1 + 1(expression)
  6. Variable: A name that is bound to a value.
  7. More of operators.

Environment Diagrams

Just a GUI simple debuger.

Functions

  1. function, args and return value
  2. 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.

  3. 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)  # 
    
  4. Note the parameters and return values of functions.
  5. Calling a function is creating a stack frame.
  6. Environment: Assembly consisting of all frames of stacks of a specific scope.
  7. 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.