Lecture 2
1.1three mechanisms a language must have:
(1)primitive statement and expression
(2)means of combination
(3)means of abstraction
We have identified in Python some of the elements that must appear in any powerful programming language:
- Numbers and arithmetic operations are primitive built-in data values and functions.
- Nested function application provides a means of combining operations.
- Binding names to values provides a limited means of abstraction.
1.2.mathematical operators
+ - * / % //(floor divide) **(square)
1.3.call expression
max(a,b)
max is operator . a,b is operand.
1.4.import libarary
e.g.
from operator import add,mul
from math import pi
1.5.names and environment
e.g.
>>> x, y = 3, 4.5 >>> y, x = x, y >>> x 4.5 >>> y 3
two important things:
An environment is asequence of frames.
A frame is a binfing between names and values.
2.1def functions(another powerful tool for abstraction)
def <name>(<formal parameters>): body return <return expression>
eg.
>>> def square(x): return mul(x, x)
2.2 environment diagrams
tutor for python
2.3 calling user-defined functions
- Bind the arguments to the names of the function's formal parameters in a new local frame.
- Execute the body of the function in the environment that starts with this frame.
(!!!distinguish the global frame and the local frame)
Appendix
new functions mentioned
add() mul() abs() pow() max() min() sub() print()