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

def build_profile(first,last,**user_info):    #**号创建空字典
"""creat a dictionary,including all of users which we know"""
  profile = {}               #程序的关键部分就是字典的建立,首先有一个空字典为接下来的键值对建立做准备
  profile['first_name'] = first           #创建字典内的键值对,通过这个方式将实参输入字典
  profile['last_name'] = last         #同上
  for key,value in user_info.items():        #以循环的方式应对任意数量形参的输入,通过方法items()将键值分别赋给key,value
    profile[key] = value        #变量赋值,将上一步分别获得的key和value进行关联
  return profile              #实际需要的是 profile 这个字典
user_profile = build_profile('Huang','Jerry',age=21,agree='college',  #注意这里的字典键值对中间是等号
              lover='Eurus Dai')
print(user_profile)            #以字典形式打印信息

 

posted on 2017-11-13 10:50  南邮21栋 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/hhjfighting/p/7825295.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值