Python基础学习(五):函数

本文介绍了Lambda函数的语法、常见用法,如赋值、与map和filter结合,并通过Cobb-Douglas生产函数展示了不同规模下的回报率概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

一、Lambda函数

提示:本章介绍lambda函数,感觉用处与列表表达式重合

1.语法

lambda函数(匿名函数)的语法只包含一个语句:

lambda [arg1 [,arg2,.....argn]]:expression

2.常见用法

(1)将lambda函数赋值给一个变量,通过变量直接调用lambda函数

jisuan = lambda x, y: x+y
for i in range(5):
    print(jisuan(i, i+1))

(2)与map函数连用

result = list(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))
print(result)

(3)与filter函数连用

newlist = filter(lambda x: x % 3 == 0, [1, 2, 3])
print(list(newlist))

3、例题

(1)与map函数连用

spells = ["protego", "accio", "expecto patronum", "legilimens"]
shout_spells_list = list(map(lambda x: x+'!!!', spells))
print(shout_spells_list)

二、函数

The following notation will be used throughout

  • Y Y Y: Output
  • L L L: Total labor units
  • K K K: Physical capital

We write a production function F F F, that transforms labor ( L L L) and capital ( K K K) into output ( Y Y Y) as

Y = F ( K , L ) = z K α L 1 − α Y = F(K, L)= z K^{\alpha} L^{1-\alpha} Y=F(K,L)=zKαL1α

1.函数定义

def cobb_douglas(K, L):

    # Create alpha and z
    z = 1
    alpha = 0.33

    return z * K**alpha * L**(1 - alpha)

If, for any K , L K, L K,L, we multiply K , L K, L K,L by a value γ \gamma γ
then

  • If Y 2 Y 1 < γ \frac{Y_2}{Y_1} < \gamma Y1Y2<γ then we say the production function has
    decreasing returns to scale.
  • If Y 2 Y 1 = γ \frac{Y_2}{Y_1} = \gamma Y1Y2=γ then we say the production function has
    constant returns to scale.
  • If Y 2 Y 1 > γ \frac{Y_2}{Y_1} > \gamma Y1Y2>γ then we say the production function has
    increasing returns to scale.
  def returns_to_scale(K, L, gamma):
    y1 = cobb_douglas(K, L)
    y2 = cobb_douglas(gamma*K, gamma*L)
    y_ratio = y2 / y1
    return y_ratio / gamma

2.函数注释

有了函数注释后,cobb_douglas?即可显示

def cobb_douglas(K, L):
    """
    Computes the production F(K, L) for a Cobb-Douglas production function

    Takes the form F(K, L) = z K^{\alpha} L^{1 - \alpha}

    We restrict z = 1 and alpha = 0.33 
    """
    return 1.0 * K**(0.33) * L**(1.0 - 0.33)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值