其他分享
首页 > 其他分享> > lambda表达式

lambda表达式

作者:互联网

lambda(匿名函数)表达式

# def fn1():
#     return 100

# lambda : 100
fn = lambda : 100  # <function <lambda> at 0x000002AAE802D268>内存地址
print(fn())  # 100


def add(a, b):
    return a + b


result = add(2, 3)
print(result)

fn1 = lambda a, b: a + b
print(fn1(1, 2))
# 无参数
fn1 = lambda : 11  # 11
print(fn1())
# 一个参数
fn1 = lambda a : a  # hello word
print(fn1('hello word'))
# 默认参数
fn1 = lambda a, b, c = 100: a + b + c  # 103
print(fn1(1, 2))  # 103
print(fn1(1, 2, 200))  # 203
# 可变参数
fn1 = lambda *args: args  #
print(fn1(1, 2, 3))  # (1, 2, 3)
# 不定长参数
fn1 = lambda **kwargs: kwargs
print(fn1(name = 'Python'))  # {'name': 'Python'}
print(fn1(name = 'Python', age = 20))  # {'name': 'Python', 'age': 20}

# 带判断
fn1 = lambda a, b: a if a > b else b # 比大小谁大输出谁
print(fn1(1, 2))  # 2

标签:fn1,参数,print,100,表达式,lambda
来源: https://blog.csdn.net/qq_44808710/article/details/120604198