新聞中心

創(chuàng)新互聯(lián)是一家專注于做網(wǎng)站、成都網(wǎng)站設(shè)計與策劃設(shè)計,曲陽網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:曲陽等地區(qū)。曲陽做網(wǎng)站價格咨詢:13518219792
函數(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決定。
相關(guān)推薦:《python視頻教程》
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"""
函數(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é)束代碼塊)。
網(wǎng)頁題目:創(chuàng)新互聯(lián)Python教程:python函數(shù)返回值是什么
文章分享:http://www.fisionsoft.com.cn/article/dhggcjg.html


咨詢
建站咨詢
