码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习(五)——定义函数

时间:2015-09-18 14:00:23      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

学习定义有返回值和无返回值的函数

#! coding:utf-8
# 定义一个没有返回值的函数
def fib1(n):
	"""print a Fibnoacci series up to n."""
	a,b=0,1
	while a<n:
		print(a,end=‘ ‘)
		a,b=b,a+b
	print()

# 定义一个有返回值的函数
def fib2(n):
	"""return a list of a Fibnoacci series up to n."""
	result=[]
	a,b=0,1
	while a<n:
		result.append(a)
		a,b=b,a+b
	return result

# 代码测试
result=fib2(20)
print(result)

运行结果:

[0, 1, 1, 2, 3, 5, 8, 13]

Python学习(五)——定义函数

标签:

原文地址:http://my.oschina.net/zzw922cn/blog/508039

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!