logo

🧪 *args 与 **kwargs

⏱️ 20-25 min📊 Intermediate
main.py
Costs 1 credit
加载编辑器中...
输出 Output执行成功
# 运行代码后,输出将显示在这里
# 按 Ctrl+Enter 快速运行
Terminal (Simulated)Install commands are simulated only, no real network requests
$

Step 1: 使用 *args

*args 适合“参数个数不固定”的场景,如批量求和、聚合统计。

底层逻辑:
调用时多个位置参数会被自动打包成元组传入 args
你可以像处理普通元组一样遍历或求和。

你的任务

  1. 定义函数 sum_all(*args)
  2. 函数返回所有参数的和
  3. 调用 sum_all(1, 2, 3, 4, 5) 并输出结果

Self-Check List

  • 使用 *args 接收多个参数

Transfer Template

def sum_all(*args):
    return sum(args)
After completing this step, you should be able to independently explain and reproduce this concept, then apply it to similar problems.

验证清单 (0/0)