return怎么读什么意思,return怎么返回两个数

  

  大蟒中如果需要函数返回多个值,可以返回一个包含多个值的元组(元组),列表(列表)或者字典(字典):   

  

  def profile(): name=' Danny ' age=30 return(name,age)profile _ data=profile()print(profile _ data 0)# output : Danny print(profile _ data 1)# output : 30或者按照更常见的惯例:   

  

  def profile(): name=' Danny ' age=30 return name,ageprofile_name,profile _ age=profile()print(profile _ name)# output : Danny print(profile _ age)# output : 30当然也可以使用具名元组(名为双重):   

  

  从集合导入namedtupledef profile(): Person=命名元组(' Person ',' name age ')返回Person(name='Danny ',age=31)# Use as named tuplep=profile()print(p,type(p))# Person(name='Danny ',age=31) class '__main__ .“人物”印刷品(姓名)#丹尼印刷品(年龄)# 31 #用作普通模式plep=profile()print(P0)# Danny print(P1)# 31 #立即打开它姓名,年龄=个人资料()打印(姓名)#丹尼打印(年龄)# 31   

相关文章