极客小将

您现在的位置是:首页 » python编程资讯

资讯内容

python的函数结果是什么

极客小将2021-01-17-
简介返回值简介简单介绍print和return的区别,print仅仅是打印在控制台,而return则是将return后面的部分作为返回值:作为函数的输出,可以用变量接走,继续使用该返回值做其它事。推荐:Python教程函数需要先定义后调用,函数体中return语句的结果就是返回值。如果一个函数没有reu

AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台


AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

返回值简介AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

简单介绍print和return的区别,print仅仅是打印在控制台,而return则是将return后面的部分作为返回值:作为函数的输出,可以用变AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

量接走,继续使用该返回值做其它事。AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

推荐:python教程AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

函数需要先定义后调用,函数体中return语句的结果就是返回值。如果一个函数没有reutrn语句,其实它有一个隐含的return语句,返回AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

值是None,类型也是'NoneType'。.AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

def func(x,y):     num = x + y     return   print(func(1,2))   #上面代码的输出结果为:None

从上面例子可以看出print( )只是起一个打印作用,函数具体返回什么由return决定AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

return语句的作用:
AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

结束函数调用、返回值AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

指定返回值与隐含返回值:
AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

1、函数体中return语句有指定返回值时返回的就是其值AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

2、函数体中没有return语句时,函数运行结束会隐含返回一个None作为返回值,类型是NoneType,与return 、return None 等效,都是返回 None。AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

def showplus(x): print(x) return x + 1 num = showplus(6) add = num + 2 print(add) #上面函数的输出结果为:6、9

隐含return None 举例:AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

def showplus(x): print(x) num = showplus(6) print(num) print(type(num)) """ 上面函数的输出结果为:6 6 None <class 'NoneType'> """

函数返回值赋值给变量:AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

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 )

更多技术请关注www.py.cn。AOl少儿编程网-Scratch_Python_教程_免费儿童编程学习平台

网友点评

共有5条评论来说两句吧...

在线客服