61a_scip_python_01
Fundamental ideas:
- represent information
- specify logic to process it
- design abstractions that manage the complexity of that logic
Three mechanisms of powerful language:
- primitive expression and statements: represent the simplest building blocks that the language provides
- means of combination: compound elements are built from simpler ones
- means of abstraction: compound elements can benamed and manipulated as units
Statements and Expressions:
- statements typically describe actions
- expressions typically describe computations
Expressions:
'''
value: the amount represented by a letter or synbol 数值 数
computation: an act or the process of calculating sth 计算 计算过程
evaluation: to form an opinion of the amount, value or quality of sth
operator: a symbol or function which represents an operation in mathematics 算子
operand: the number on which an operation is to be done 运算数
'''
- an expression describe a computation and evaluate to value
- all expression can use function call notation
- primitive expressions:
- number: the expression you type consists of the numerals that represent the number in base 10
- names
- string
Call expressions:
- structure: operator(operand,operand...) max(1,2)
- operator and operand are also expressions. name or numbers or subexpressions?
- evaluation procedure for call expressions:
- evaluate the operator to get function and then operand subexpression to get arguments
- apply the fuction that is the value of the the operator subexpression to the arguments that are the values of the operand subexpressions
- evaluate nested expressions: use expression tree
mul(add(4,mul(4,6)),add(3,5))
Names:
- use names to refer to computational objects
- if a value has been given a name, we say that name binds to the value
- how to bind:
- assigment statement: (name = expression) evaluate expression and bind it to the name
- import statement: just built-in names
- def statement: def a fuction
- names are often called variable names or variables because they can be bound to different values but just bind one
- change the value of one name does not affect other name because after evaluation just value to be bound to the name
x=2 y=x+1 x=3 print(y)
but the function
def f(a): return a+1 x=2 y= f(x) print(y) x=3 print(y)
because return statement, it get re-revaluated everytime it's called
- environment:
- environments are real things, they're the way in which an interpreter for programming language keeps track of what names mean, so it's sort of menory that keeps track of the bindings between names and values