首页 > 编程语言 > 详细

Python基础教程笔记——第6章:抽象(函数)

时间:2015-08-08 10:18:44      阅读:290      评论:0      收藏:0      [点我收藏+]

(1)计算裴波那契数列:

1 fbis=[0,1]
2 num=int(input("please input the number"))
3 for i in range(num-2):
4     fbis.append(fbis[-2]+fbis[-1])
5 print(fbis)      

6.3 创建函数

callable():函数可以确定函数是否可以被调用

最简单的例子:

 

1 def hello(name):
2     return "hello"+name+"are you OK?"

 

输入个数,输出裴波那契数列的函数

 1 def fbis(num):
 2     if num<=0:
 3         return "input error"
 4     if num==1:
 5         return [0]
 6     if num==2:
 7         return [0,1]
 8     f=[0,1]
 9     for i in range(num-2):
10         f.append(f[-1]+f[-2])
11     return f

记录函数:给函数写说明文档和查看函数的说明文档

 

1 def hello(name):
2     this is a print function#函数说明文档作为字符串
3     print("hello "+name)
4 
5 #查看函数文档
7 >>> hello.__doc__
8 this is a print function

 

6.4 函数参数

函数的参数和C,C++一样也是值传递

 

Python基础教程笔记——第6章:抽象(函数)

原文:http://www.cnblogs.com/yanliang12138/p/4712601.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!