python 函数中多个return 列表,元组,字典 | 您所在的位置:网站首页 › 函数列表怎么列 › python 函数中多个return 列表,元组,字典 |
#每天一点点# python 函数中多个return 列表,元组,字典 return 结束函数的执行 #函数中多个return def test(): a = 11 b = 22 c = 33 return a return b num1 = test() print(num1) num2 = test() print(num2)输出结果→→→→→ 11 11 return 多个变量不会报错,但只执行第一个return的内容 明明定义了两个函数啊,为什么没有输出 22 ?? 因为return 结束函数的执行,每次调用函数,return 了 a,然后,就没有然后了啊啊啊 当有多个变量时,怎么return呢??? 方法1: 用列表来封装多个变量额值 def test(): a = 11 b = 22 c = 33 d = [a,b,c] #用一个列表来封装3个变量的值 return d num1 = test() print(num1)输出结果→→→→→ [11, 22, 33] 返回一个列表 方法2:不用列表封装,直接返回变量 def test(): a = 11 b = 22 c = 33 return [a,b,c] #不需要列表封装,直接以列表的形式return num1 = test() print(num1)输出结果→→→→→ [11, 22, 33] 返回一个列表 方法3:列表可以,那么元组也同理 def test(): a = 11 b = 22 c = 33 return (a,b,c) #元组同理 效果与 return a,b,c 相同 num1 = test() print(num1)输出结果→→→→→ (11, 22, 33) 方法4:字典也同理 def test(): a = 11 b = 22 c = 33 return {a,b,c} num1 = test() print(num1)输出结果→→→→→ {33, 11, 22} |
CopyRight 2018-2019 实验室设备网 版权所有 |