标签:class 判断 基础 war 算法 fun 可变参数 **kwargs 条件
# 3 + 2 + 1 def sum_numbers(num): # 1.如果是1,直接返回1 -- 出? if num == 1: return 1 # 2.如果不是1,重复执?累加并返回结果 return num + sum_numbers(num-1) sum_result = sum_numbers(3) # 输出结果为6 print(sum_result)
lambda 参数列表 : 表达式
# 函数 def fn1(): return 200 print(fn1) print(fn1()) # lambda表达式 fn2 = lambda: 100 print(fn2) print(fn2())
def add(a, b): return a + b result = add(1, 2) print(result)
fn1 = lambda a, b: a + b print(fn1(1, 2))
fn1 = lambda: 100 print(fn1())
fn1 = lambda a: a print(fn1(‘hello world‘))
fn1 = lambda a, b, c=100: a + b + c print(fn1(10, 20))
fn1 = lambda *args: args print(fn1(10, 20, 30))
fn1 = lambda **kwargs: kwargs print(fn1(name=‘python‘, age=20))
fn1 = lambda a, b: a if a > b else b print(fn1(1000, 500))
students = [ {‘name‘: ‘TOM‘, ‘age‘: 20}, {‘name‘: ‘ROSE‘, ‘age‘: 19}, {‘name‘: ‘Jack‘, ‘age‘: 22} ] # 按name值升序排列 students.sort(key=lambda x: x[‘name‘]) print(students) # 按name值降序排列 students.sort(key=lambda x: x[‘name‘], reverse=True) print(students) # 按age值升序排列 students.sort(key=lambda x: x[‘age‘]) print(students)
abs(-10) # 10
round(1.2) # 1 round(1.9) # 2
def add_num(a, b): return abs(a) + abs(b) result = add_num(-1, 2) print(result) # 3
def sum_num(a, b, f): return f(a) + f(b) result = sum_num(-1, 2, abs) print(result) # 3
list1 = [1, 2, 3, 4, 5] def func(x): return x ** 2 result = map(func, list1) print(result) # <map object at 0x0000013769653198> print(list(result)) # [1, 4, 9, 16, 25]
import functools list1 = [1, 2, 3, 4, 5] def func(a, b): return a + b result = functools.reduce(func, list1) print(result) # 15
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] def func(x): return x % 2 == 0 result = filter(func, list1) print(result) # <filter object at 0x0000017AF9DC3198> print(list(result)) # [2, 4, 6, 8, 10]
lambda 参数列表: 表达式
标签:class 判断 基础 war 算法 fun 可变参数 **kwargs 条件
原文地址:https://www.cnblogs.com/qiu-hua/p/14727842.html