代码示例库

搜索、筛选并收藏你需要的 Python 代码片段。

重置
#入门 #字符串 #数字 #列表 #列表 #循环 #切片 #if #列表 #字典 #字典 #while #while #函数 #函数 #类 #类 #文件 #异常 #测试 #测试 #Pygame #Pygame #matplotlib #随机漫步 #CSV #API #Django #Django #Django

函数与返回值

第8章 函数

演示函数定义、默认值与返回值。

def greet_user(username="匿名用户"):
    print(f"你好,{username.title()}!")

greet_user("jesse")
greet_user()

def get_formatted_name(first, last):
    full = f"{first} {last}"
    return full.title()

name = get_formatted_name("john", "smith")
print(name)

任意数量实参

第8章 函数

演示*args与**kwargs。

def make_pizza(size, *toppings):
    print(f"制作{size}寸披萨,配料: ")
    for topping in toppings:
        print(f"- {topping}")

make_pizza(12, "mushrooms", "pepperoni")

def build_profile(first, last, **user_info):
    profile = {"first": first, "last": last}
    for k, v in user_info.items():
        p
...