def test1():
    '以元组的形式返回'
    return 1,2,3
    
d=test1()
print(d)
print(d[0])
print(test1()[1])

#输出结果:
#(1, 2, 3)
#1
#2
def test2():
    return 1,2,3
a,b,c=test2()
print(a,b)
print(c)
d=test2()
print(d)

#1 2
#3
#(1,2,3)

return的返回值可以自定义返回形式,可以是列表或者元组。如果没有自定义,将以元组的形式返回结果。

 新建一个函数
In [1]: def nums():
   ...:     a = 11
   ...:     b = 22
   ...:     c = 33
   ...:     return [a,b,c] #以列表的方式返回
   ...: #调用函数
   ...: nums()
   ...: 
   ...: #函数返回值
Out[1]: [11, 22, 33]


# 新建一个函数
In [2]: def nums():
   ...:     a = 11
   ...:     b = 22
   ...:     c = 33
   ...:     return (a,b,c) #以元祖的方式返回
   ...: #调用函数
   ...: nums()
   ...: 
   ...: #函数返回值
Out[2]: (11, 22, 33)


# 新建一个函数
In [3]: def nums():
   ...:     a = 11
   ...:     b = 22
   ...:     c = 33
   ...:     return a,b,c #以元祖的方式返回的另一种形式
   ...: #调用函数
   ...: nums()
   ...: 
   ...: #函数返回值
Out[3]: (11, 22, 33)

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-22
  • 2022-12-23
  • 2021-11-06
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-25
  • 2021-06-13
  • 2022-01-17
  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案