python中如何定义函数返回值


当前第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中如何定义函数返回值的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

静态方法怎么使用

cookie介绍和模拟登录演示

django教程中user-profile的使用方法介绍(附源码)

爬虫是什么?对爬虫的详解

pycharm和Python区别是什么

Python怎么把三位数拆开

Python实现按照指定要求逆序输出一个数字

pyqt5每天必学之带有标签的复选框

Python 怎么调用函数

介绍Python3.6简单操作mysql数据库的方法

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...