python函数传递参数

1.传递任意数量的关键字实参:符号为**
def build_porfile(first,last,**user_info):
profile={}
profile[‘first_name’]=first
profile[‘lase_name’]=last
for key,value in user_info.items():
profile[key]=value
return profile

user_info=build_porfile(‘albert’,‘einstein’,location=‘prinecton’,field=‘physics’)

print(user_info)

2.传递任意数量的实参:符号为*
#def sandwitch(*ingredents):
print(“The ingredient of the sandwitch including”)
for ingredent in ingredents:
print(ingredent)
sandwitch(‘cabbage’,‘prock’,‘beef’)
sandwitch(‘mutton’,‘fish’)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,函数之间可以通过位置传参和关键字传参传递参数。位置传参是指按照参数的位置顺序传递参数值,而关键字传参是指通过参数名来传递参数值。 在位置传参中,可以使用*来解包元组参数。这意味着将元组的每个元素分别传递给函数的位置参数。例如,在调用函数时使用*args,可以将args拆分为独立的三个元素,分别传递给函数的a、b、c参数。如果在调用函数时没有使用*,则args将作为一个整体传递给函数,可能导致参数传递错误。 在关键字传参中,可以使用**来解包字典参数。这意味着将字典的键值对作为关键字参数传递给函数。例如,在调用函数时使用**kwargs,可以将kwargs解包为多个关键字参数传递给函数。 此外,还可以使用缺省参数来指定函数的默认参数值。这样,在调用函数时如果没有传递对应的参数值,将使用默认值。 总结起来,Python函数之间可以通过位置传参、关键字传参、解包元组参数和解包字典参数传递参数。这些方法可以根据具体的需求和函数定义来选择使用。 #### 引用[.reference_title] - *1* *3* [python 函数传递参数的多种方法](https://blog.csdn.net/dingmizhan4619/article/details/101732903)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python函数的五种参数传递的方式](https://blog.csdn.net/qq_63786218/article/details/126690008)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值