CS61A Lecture 2 Functions

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:

  1. Numbers and arithmetic operations are primitive built-in data values and functions.
  2. Nested function application provides a means of combining operations.
  3. 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

  1. Bind the arguments to the names of the function's formal parameters in a new local frame.
  2. 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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值