Python?return函數(shù)返回值類型和幫助函數(shù)使用教程
引言
經過函數(shù)學習之后我們會發(fā)現(xiàn)函數(shù)不被調用是不會直接執(zhí)行的,我們在之前的函數(shù)調用之后發(fā)現(xiàn)運行的結果都是函數(shù)體內print()打印出來的結果,但是有時候為了方便函數(shù)參與二次運算,我們讓函數(shù)體內不輸出任何結果,而是把函數(shù)本身就當做一種結果,輸出這種結果的方式就可以理解為返回函數(shù)的結果,python用return關鍵詞來返回。下面我們對比幾種不同的函數(shù)調用結果。
一、函數(shù)的輸出方式對比
1.直接使用print打印函數(shù)運行結果:直接調用函數(shù)名傳參即可。
def func1(a, b):
res = a + b
print(res)
func1(4, 9)
返回結果:132.打印沒有返回值,沒有輸出代碼塊的函數(shù),需要把函數(shù)當做一個變量來用print輸出。
def func2(a, b):
res = a + b
print(func2(4, 9))
返回結果:None3.打印有返回值(return)的函數(shù),同上,也是把函數(shù)當做一個變量來輸出。
def func3(a, b):
res = a + b
return res
# print(a) # return后面的代碼不會被執(zhí)行
print(func3(4, 9))
返回結果:13對比上面三種形式的函數(shù),如果我們想用函數(shù)的結果來做運算的話,第一種情況就無法實現(xiàn),比如
func1(4, 9) * 3 返回結果: TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
第二種情況本身就是None,所以忽略,第三種情況我們再試試
print(func3(4, 9) * 3) 返回結果:39
從上面的結果可以看出,有返回值的函數(shù)用起來很方便,直接可以當做變量來使用。
二、return的作用
同時return還有結束函數(shù)代碼塊的功能,return之后的下一行語句不會被執(zhí)行。
注意:有返回值的函數(shù)一般直接調用函數(shù)名是不執(zhí)行任何結果的,賦值給變量后才會返回結果。如果一個函數(shù)沒有return語句,其實它有一個隱含的語句,返回值是None,類型也是'None Type'。print是打印在控制臺,而return則是將后面的部分作為返回值。”
下面再來看看return的一些特別之處。
1.可以return多個結果
def func3(a, b):
res1 = a + b
res2 = a - b
return res1, res2
print(func3(4, 9))
返回結果:13? -52.一個函數(shù)可以有多個return,但是只會執(zhí)行第一個
def func3(a, b):
res1 = a + b
res2 = a - b
return res1
return res2
print(func3(4, 9))
返回結果:133.沒有return的函數(shù)返回NoneType
def func3(a, b):
res1 = a + b
res2 = a - b
print(type(func2(4, 9)))
返回結果:<class 'NoneType'>三、幫助函數(shù)
這里屬于一個補充知識點,我們在函數(shù)使用的時候不知道傳參和函數(shù)的其他用法的時候可以使用help()函數(shù)來輸出開發(fā)文檔中的文本提示。
help(print)import os #文件目錄操作模塊
os.mkdir('123')
help(os.mkdir)返回結果:
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Help on built-in function mkdir in module nt:
mkdir(path, mode=511, *, dir_fd=None)
Create a directory.If dir_fd is not None, it should be a file descriptor open to a directory,
and path should be relative; path will then be relative to that directory.
dir_fd may not be implemented on your platform.
If it is unavailable, using it will raise a NotImplementedError.The mode argument is ignored on Windows.
以上是關于Python函數(shù)返回值類型和幫助函數(shù)的講解,更多關于Python return幫助函數(shù)的資料請關注腳本之家其它相關文章!
相關文章
解決python 輸出到csv 出現(xiàn)多空行的情況
這篇文章主要介紹了解決python 輸出到csv 出現(xiàn)多空行的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python bisect_left 函數(shù)使用場景詳解
在Python的編程世界中,數(shù)據(jù)處理和搜索操作是非常常見的任務,bisect_left函數(shù)是Python標準庫bisect模塊中的一個強大工具,接下來,我們將詳細探討bisect_left函數(shù)的使用場景,需要的朋友可以參考下2024-11-11
pytest使用@pytest.mark.parametrize()實現(xiàn)參數(shù)化的示例代碼
這篇文章主要介紹了pytest使用@pytest.mark.parametrize()實現(xiàn)參數(shù)化,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
Python實現(xiàn)數(shù)據(jù)集劃分(訓練集和測試集)
這篇文章主要為大家詳細介紹了Python是如何實現(xiàn)數(shù)據(jù)集劃分的,分為訓練集和測試集,文中的實現(xiàn)方法講解詳細,感興趣的小伙伴可以了解一下2023-05-05
python selenium 無界面瀏覽器的實現(xiàn)
有時我們不想讓瀏覽器窗口跳出來,而是想在后臺進行操作,這就需要用到無界面瀏覽器,本文主要介紹了python selenium 無界面瀏覽器的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-10-10

