Python学习小记3-传递任意数量的实参

1.形参名*toppings 中的星号让Python创建一个名为toppings 的空元组,不管调用语句提供了多少实参,这个形参会将它们统统收入囊中,即:无论几个小料

def make_pizza(size, *toppings):
    print(f"\n要制作一个{size}-inch的披萨,内含的小料如下:")
    # 循环输出小料
    for topping in toppings:
        print(f"{topping}")


make_pizza(16, 'pepperoni')
make_pizza(12,  'mushrooms', 'green peppers', 'extra cheese')

 

2.形参**user_info 中的两个星号让Python创建一个名为user_info 的空字典,并将收到的所有名称值对都放到这个字典中。

def build_profile(first, last, **user_info):
    # 把first参数传给键first_name
    user_info['first_name'] = first
    user_info['last_name'] = last
    return user_info


def receive_sandwich(*sandwichs):
    print("该三明治中有以下食材:")
    for sandwich in sandwichs:
        print(f"{sandwich}")


receive_sandwich('egg', 'water', 'peach')
receive_sandwich('book')
receive_sandwich('finger', 'phone', 'shop', 'glasses')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值