Python?return函數(shù)返回值類型和幫助函數(shù)使用教程
引言
經(jīng)過(guò)函數(shù)學(xué)習(xí)之后我們會(huì)發(fā)現(xiàn)函數(shù)不被調(diào)用是不會(huì)直接執(zhí)行的,我們?cè)谥暗暮瘮?shù)調(diào)用之后發(fā)現(xiàn)運(yùn)行的結(jié)果都是函數(shù)體內(nèi)print()打印出來(lái)的結(jié)果,但是有時(shí)候?yàn)榱朔奖愫瘮?shù)參與二次運(yùn)算,我們讓函數(shù)體內(nèi)不輸出任何結(jié)果,而是把函數(shù)本身就當(dāng)做一種結(jié)果,輸出這種結(jié)果的方式就可以理解為返回函數(shù)的結(jié)果,python用return關(guān)鍵詞來(lái)返回。下面我們對(duì)比幾種不同的函數(shù)調(diào)用結(jié)果。
一、函數(shù)的輸出方式對(duì)比
1.直接使用print打印函數(shù)運(yùn)行結(jié)果:直接調(diào)用函數(shù)名傳參即可。
def func1(a, b):
res = a + b
print(res)
func1(4, 9)
返回結(jié)果:132.打印沒有返回值,沒有輸出代碼塊的函數(shù),需要把函數(shù)當(dāng)做一個(gè)變量來(lái)用print輸出。
def func2(a, b):
res = a + b
print(func2(4, 9))
返回結(jié)果:None3.打印有返回值(return)的函數(shù),同上,也是把函數(shù)當(dāng)做一個(gè)變量來(lái)輸出。
def func3(a, b):
res = a + b
return res
# print(a) # return后面的代碼不會(huì)被執(zhí)行
print(func3(4, 9))
返回結(jié)果:13對(duì)比上面三種形式的函數(shù),如果我們想用函數(shù)的結(jié)果來(lái)做運(yùn)算的話,第一種情況就無(wú)法實(shí)現(xiàn),比如
func1(4, 9) * 3 返回結(jié)果: TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
第二種情況本身就是None,所以忽略,第三種情況我們?cè)僭囋?/p>
print(func3(4, 9) * 3) 返回結(jié)果:39
從上面的結(jié)果可以看出,有返回值的函數(shù)用起來(lái)很方便,直接可以當(dāng)做變量來(lái)使用。
二、return的作用
同時(shí)return還有結(jié)束函數(shù)代碼塊的功能,return之后的下一行語(yǔ)句不會(huì)被執(zhí)行。
注意:有返回值的函數(shù)一般直接調(diào)用函數(shù)名是不執(zhí)行任何結(jié)果的,賦值給變量后才會(huì)返回結(jié)果。如果一個(gè)函數(shù)沒有return語(yǔ)句,其實(shí)它有一個(gè)隱含的語(yǔ)句,返回值是None,類型也是'None Type'。print是打印在控制臺(tái),而return則是將后面的部分作為返回值。”
下面再來(lái)看看return的一些特別之處。
1.可以return多個(gè)結(jié)果
def func3(a, b):
res1 = a + b
res2 = a - b
return res1, res2
print(func3(4, 9))
返回結(jié)果:13? -52.一個(gè)函數(shù)可以有多個(gè)return,但是只會(huì)執(zhí)行第一個(gè)
def func3(a, b):
res1 = a + b
res2 = a - b
return res1
return res2
print(func3(4, 9))
返回結(jié)果:133.沒有return的函數(shù)返回NoneType
def func3(a, b):
res1 = a + b
res2 = a - b
print(type(func2(4, 9)))
返回結(jié)果:<class 'NoneType'>三、幫助函數(shù)
這里屬于一個(gè)補(bǔ)充知識(shí)點(diǎn),我們?cè)诤瘮?shù)使用的時(shí)候不知道傳參和函數(shù)的其他用法的時(shí)候可以使用help()函數(shù)來(lái)輸出開發(fā)文檔中的文本提示。
help(print)import os #文件目錄操作模塊
os.mkdir('123')
help(os.mkdir)返回結(jié)果:
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.
以上是關(guān)于Python函數(shù)返回值類型和幫助函數(shù)的講解,更多關(guān)于Python return幫助函數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python2和python3實(shí)現(xiàn)在圖片上加漢字的方法
python2和python3實(shí)現(xiàn)在圖片上加漢字,最主要的區(qū)別還是內(nèi)部編碼方式不一樣導(dǎo)致的,在代碼上表現(xiàn)為些許的差別。這篇文章主要介紹了python2和python3實(shí)現(xiàn)在圖片上加漢字,需要的朋友可以參考下2019-08-08
python實(shí)現(xiàn)對(duì)指定輸入的字符串逆序輸出的6種方法
這篇文章主要介紹了python實(shí)現(xiàn)對(duì)指定輸入的字符串逆序輸出的6種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
python uuid生成唯一id或str的最簡(jiǎn)單案例
這篇文章主要介紹了python uuid生成唯一id或str的最簡(jiǎn)單案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
Python 實(shí)現(xiàn)Image和Ndarray互相轉(zhuǎn)換
今天小編就為大家分享一篇Python 實(shí)現(xiàn)Image和Ndarray互相轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python時(shí)間差中seconds和total_seconds的區(qū)別詳解
今天小編就為大家分享一篇Python時(shí)間差中seconds和total_seconds的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
pandas數(shù)據(jù)類型之Series的具體使用
本文主要介紹了pandas數(shù)據(jù)類型之Series的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

