0基础学python(25)

本文探讨了如何在函数中有效地传递列表,包括遍历列表进行问候,修改列表内容,以及处理任意数量和类型的位置实参。重点讲解了混合使用位置参数、关键字参数和星号参数的技巧,帮助读者理解Python函数的灵活性。
摘要由CSDN通过智能技术生成
传递列表

在使用函数一段时间就会发现,列表在函数中的用处很大,其中可能包含很复杂的名字,数,我们将列表传递给函数后,函数就能直接访问其内容。
加入我们有一个用户列表,我们要问候列表里面每一位用户。
例如:

def greet_user(names):
    for name in names:
        msg=f"hello,{name.title()}"
        print(msg)

usernames=['hannah','ty','margot']
greet_user((usernames))

我们将greet_users()定义为一个接受一个名字列表,并且将其赋值给了形参names函数遍历每一句,并且对每个用户都输出一句问候语。

hello,Hannah
hello,Ty
hello,Margot
在函数中修改列表

将列表传递给函数后,函数就可以对其进行修改。
例如:

def print_models(unprinted_desings,completed_models):
    while unprinted_desings:
        current_design=unprinted_desings.pop()
        print(f"printing model:{current_design}")
        completed_models.append(current_design)
def show_completed_models(completed_models):
    print("\nthe followsing 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_completed_models(completed_models)
printing model:dodecahedron
printing model:robot pendant
printing model:phone case

the followsing models have been printed:
dodecahedron
robot pendant
phone case
传递任意数量的实参

有时我们不知道函数需要接受多少实参,但是python允许函数收集任意数量的实参。
例如,我们制作一个披萨的函数,我们可能需要很多不同的配料,但是无法确定要哪个配料。

def make_pizza(*toppings):
    print(toppings)

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

形参toppings中的好让python创建一个名为toppings的空元组,并将收到的所有值都装到这个元组中,之后痛函数体内的函数调用print()通过生成输出,这样既能处理一个值也能处理三个值

('pepperoni',)
('mushrooms', 'green peppers', 'extra cheese')

现在可以将函数用print()替换一个循环,遍历配料列表并对顾客点的披萨进行描述:

def make_pizza(*toppings):
    print("\nmaking a pizza with the following toppongs")
    for topping in toppings:
        print(f"-{topping}")

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

不管是一个值还是三个值python都能妥善的处理,这个方法非常简便好用。

making a pizza with the following toppongs
-pepperoni

making a pizza with the following toppongs
-mushrooms
-green peppers
-extra cheese
结合使用位置实参和任意数量实参

如果想要让函数接受不同类型的实参,必须要把接纳任意数量的实参放到后面,先让python匹配位置实参,最后将剩下的都收集到任意数量 的实参中。
例如,我们想要给pizza加上一个尺寸。

def make_pizza(size,*toppings):
    print(f"\nmaking a {size}-inch pizza with the following toppongs")
    for topping in toppings:
        print(f"-{topping}")

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

我们将第一个形参给到了size将后面任意数量的形参都给了*toppings

making a 16-inch pizza with the following toppongs
-pepperoni

making a 12-inch pizza with the following toppongs
-mushrooms
-green peppers
-extra cheese

也许你经常会看到通用形参名*args,他也收集任意数量的位置实参。

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

有时候需要接受任意数量 的实参,但是不知道传递给函数的会是什么样的信息,我们可以将函数编写成能够接受任意数量的键值对。但是键值对必须放到后面。
例如;

def build_profile(first,last,**user_info):
    user_info['first_name']=first
    user_info['last_name']=last
    return user_info

user_profile=build_profile('albert','einstein',
                           location='princeton',
                           field='pysics')
print(user_profile)
{'location': 'princeton', 'field': 'pysics', 'first_name': 'albert', 'last_name': 'einstein'}

在我们编写程序的时候能够以各种方式混合使用位置实参,关键字实参和任意数量的实参。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

the best b

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值