Python函数返回值实例分析

WBOY
Release: 2016-06-06 11:18:56
Original
1246 people have browsed it

本文实例讲述了Python的函数返回值用法。分享给大家供大家参考。具体分析如下:

最近学一些Python的基本用法,看到定义函数,发现似乎只能返回一个返回值,想想matlab里返回多个返回值多方便啊,网上查了查,看到有返回多个值的方法。
 
python 函数返回值有两种形式:

1 返回一个值。
2 返回多个值。
 
现看看返回一个值的吧。

def firstvalue(a,b):
 c = a + b
 return c
print firstvalue(1,2) #结果:3

Copy after login

再看看返回多个值的: 那怎么可以返回多个值呢,其他的语言一般调用函数的话,只能返回一个值,可能我不太熟悉所有的语言,我知道的语言只能返回一个值,而python可以返回多个值,感觉非常方便,发代码看下:

def secondvalue(a,b):
 c = a + b
 return (a,b,c)
x,y,z = secondvalue(1,2)
print 'x:',x,'y:',y,'z:',z

Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!