当前第2页 返回上一页
1 2 3 4 5 6 7 8 9 | def showplus(x):
print (x)
return x + 1
print (x + 1) #该语句会执行么
print (showplus(6))
输出结果:
6
7
|
返回值类型
无论定义的是返回什么类型,return 只能返回单值,但值可以存在多个元素;
return [1,3,5] 是指返回一个列表,是一个列表对象,1,3,5 分别是这个列表的元素;
return 1,3,5 看似返回多个值,隐式地被Python封装成了一个元祖返回
例1:
1 2 3 4 5 6 7 | def showlist():
return [1,3,5] #多元素,返回的是什么类型
print (type(showlist()))
print (showlist())
输出结果:
< class 'list' >
[1, 3, 5] #列表类型
|
例2:
1 2 3 4 5 6 | def showlist():
return 2,4,6 #多值时,不指定类型
print (type(showlist()))
print (showlist())
输出结果:
< class 'tuple' > #默认封装成元祖类型
|
更多Python相关技术文章,请访问Python教程栏目进行学习!
以上就是python中如何定义函数返回值的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
Python2和Python3字符串区别
如何把字符串转化成时间
Python回车不能换行怎么办
Python需要什么开发环境及电脑的配置
Python中赋值与c语言区别
Python if有多个条件怎么办
Python之中正则表达式详解(实例分析)
Python中enumerate什么意思
有关django模板无法使用perms变量问题的解决方法
Python的命名规范是什么?Python命名规范的介绍
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python中如何定义函数返回值