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

返回前面的内容

相关阅读 >>

Python是爬虫吗

Python编程如何求2000到2500闰年?

java和Python的区别

Python如何运行一个Python程序

Python requests快速入门介绍

有什么是用Python做的

object怎么转换成float数据

Python matplotlib中文显示参数设置解析_Python

pandas 实现将重复表格去重,并重新转换为表格

Python如何调用api接口

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




打赏

取消

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

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

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

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

评论

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

    正在狠努力加载,请稍候...