創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

這篇文章給大家分享的是有關(guān)python中函數(shù)返回值的使用示例的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
函數(shù)返回值簡介
1、簡單介紹print和return的區(qū)別,print僅僅是打印在控制臺,而return則是將return后面的部分作為返回值:作為函數(shù)的輸出,可以用變量接走,繼續(xù)使用該返回值做其它事。
2、函數(shù)需要先定義后調(diào)用,函數(shù)體中return語句的結(jié)果就是返回值。如果一個函數(shù)沒有reutrn語句,其實它有一個隱含的return語句,返回值是None,類型也是'NoneType'。.
def func(x,y): num = x + y return print(func(1,2)) #上面代碼的輸出結(jié)果為:None
從上面例子可以看出print( )只是起一個打印作用,函數(shù)具體返回什么由return決定
return語句的作用:
結(jié)束函數(shù)調(diào)用、返回值
指定返回值與隱含返回值:
1、函數(shù)體中return語句有指定返回值時返回的就是其值
2、函數(shù)體中沒有return語句時,函數(shù)運行結(jié)束會隱含返回一個None作為返回值,類型是NoneType,與return 、return None 等效,都是返回 None。
def showplus(x): print(x) return x + 1 num = showplus(6) add = num + 2 print(add) #上面函數(shù)的輸出結(jié)果為:6、9
隱含return None 舉例:
def showplus(x): print(x) num = showplus(6) print(num) print(type(num)) """ 上面函數(shù)的輸出結(jié)果為:6 6 None <class 'NoneType'> """
函數(shù)返回值賦值給變量:
import os
import sys
import subprocess
def get_manifest_xml_path():
xml_path = input()
if os.path.exists( xml_path ):
return xml_path
else:
print('AndroidManifest.xml not found!')
def get_out_path( xml_path ):
return os.path.dirname( os.path.abspath( xml_path ) ) + os.sep + 'AndroidManifest.txt'
def convert_xml_to_txt( xml_path, out_path ):
convert_cmd = 'java -jar AXMLPrinter2.jar %s>%s' % ( xml_path, out_path )
subprocess.Popen( convert_cmd, shell=True )
if __name__ == "__main__":
xml_path = get_manifest_xml_path()
out_path = get_out_path( xml_path )
convert_xml_to_txt( xml_path, out_path )return 語句位置與多條 return 語句
1、python函數(shù)使用return語句返回 "返回值",可以將其賦給其它變量作其它的用處
2、所有函數(shù)都有返回值,如果沒有return語句,會隱式地調(diào)用 return None 作為返回值;
3、一個函數(shù)可以存在多條return語句,但只有一條可以被執(zhí)行,如果沒有一條reutrn語句被執(zhí)行,同樣會隱式調(diào)用return None作為返回值;
4、如果有必要,可以顯式調(diào)用return None明確返回一個None(空值對象)作為返回值,可以簡寫為return,不過python中懶惰即美德,所以一般能不寫就不寫;
5、如果函數(shù)執(zhí)行了return語句,函數(shù)會立刻返回,結(jié)束調(diào)用,return之后的其它語句都不會被執(zhí)行了(可用于結(jié)束代碼塊)。
感謝各位的閱讀!關(guān)于python中函數(shù)返回值的使用示例就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
當前題目:python中函數(shù)返回值的使用示例-創(chuàng)新互聯(lián)
瀏覽路徑:http://chinadenli.net/article28/digocp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、手機網(wǎng)站建設(shè)、網(wǎng)站排名、網(wǎng)站策劃、網(wǎng)站收錄、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容