python基础之函数返回多个值的方法

例:

>>> def test():
	a=11
	b=22
	c=33
	return a  #多个return,语法不会报错,但是只执行第一个return
	return b  #不会执行此条语句
	return c  #不会执行此条语句

>>> num=test()
>>> num
11

要想获得多个返回值,可以用列表或元组封装

>>> def test():
	a=11
	b=22
	c=33
	return [a,b,c]

>>> num=test()
>>> num
[11, 22, 33]
>>> def test():
	a=11
	b=22
	c=33
	return a,b,c

>>> num=test()
>>> num
(11, 22, 33)
>>> def test():
	a=11
	b=22
	c=33
	return (a,b,c)

>>> num=test()
>>> num
(11, 22, 33)

猜你喜欢

转载自blog.csdn.net/Panda996/article/details/84786649
今日推荐