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)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章