python 函数(第三节)

一、传递列表

将列表传递给函数后,函数就能直接访问其内容。提高了处理效率。

def greet_users(names):
    for name in names:
        msg = f"Hello,{name.title()}!"
        print(msg)
username = ['hannah','ty','margot']#定义了一个列表
greet_users(username)#将列表传递给函数greet_users
#结果输出
Hello,Hannah!
Hello,Ty!
Hello,Margot!

1、在函数中修改列表

在函数中对列表所做的修改都是永久性的。更容易扩展和维护。

def print_models(unprinted_designs, completed_models):
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        print(f"Printing model: {current_design}")
#将打印的设计移动到列表completed_models中
        completed_models.append(current_design)
def show_complete_models(completed_models):
    print("\nThe following models have been printed:")
#显示所有打印好的模型
    for completed_model in completed_models:
        print(completed_model)
unprinted_designs = ['phone case', 'robot pendant', 'dodecahedron']
completed_models = []

#模拟打印设计的过程
print_models(unprinted_designs, completed_models)
#指出打印了那些模型
show_complete_models(completed_models)
#结果输出
Printing model: dodecahedron
Printing model: robot pendant
Printing model: phone case

The following models have been printed:
dodecahedron
robot pendant
phone case

2、禁止函数修改列表

有时可能会遇到:“即使打印好了所有设计,也要保留原来未打印的设计列表”。此时需要向函数传递副本而不是原件。利用切片[:]创建列表的副本。

def print_models(unprinted_designs, completed_models):
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        print(f"Printing model: {current_design}")
        completed_models.append(current_design)
def show_complete_models(completed_models):
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)
unprinted_designs = ['phone case', 'robot pendant', 'dodecahedron']
completed_models = []


print_models(unprinted_designs[:], completed_models)
show_complete_models(completed_models)
#原件依旧可以查询到
print(unprinted_designs)
#结果输出
Printing model: dodecahedron
Printing model: robot pendant
Printing model: phone case

The following models have been printed:
dodecahedron
robot pendant
phone case
['phone case', 'robot pendant', 'dodecahedron']

二、传递任意数量的实参

python允许函数从调用中收集任意数量的实参。在不知道需要多少个实参时,用表示符“*形参”。

def make_pizza(*toppings):
#"*"让python创建一个空元组,并将收到的所有值都封装到这个元组中
    print("\nMaking a pizza with the following topping:")
    for topping in toppings:
        print(f"-{topping}")
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
#结果输出
Making a pizza with the following topping:
-pepperoni

Making a pizza with the following topping:
-mushrooms
-green peppers
-extra cheese

1、结合使用位置实参和任意数量实参

要让函数接受不同类型的实参,需要将函数定义中接纳任意数量实参的形参房租最后。

def make_pizza(size,*toppings):
    print(f"Making a {size}-inch pizza with the foolowing toppings:")
    for topping in toppings:
        print(f"-{topping}")
make_pizza(16,'pepperoni')
make_pizza(12,'mushrooms', 'green pepers', 'extra cheese')
#结果输出
Making a 16-inch pizza with the foolowing toppings:
-pepperoni
Making a 12-inch pizza with the foolowing toppings:
-mushrooms
-green pepers
-extra cheese

注:*args:收集任意数量的位置实参 

2、使用任意数量的关键字实参

需要接收任意数量的实参,但不知道传递给函数的会是什么样的信息。在这种情况下,将函数编写成能够接受任意数量的键值对,调用语句提供了多少就可以接受多少。

def buid_profile(first, last, **user_info):
    user_info['first_name'] = first
    user_info['last_name'] = last
    return user_info
user_profile = buid_profile('albert', 'einstein',
                            location='princeton',
                            field='physics')
print(user_profile)
#结果输出
{'location': 'princeton', 'field': 'physics', 'first_name': 'albert', 'last_name': 'einstein'}

注:**kwargs:收集任意数量的关键字实参

此试题皆出自python编程-从入门到实践

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值