# -*- coding: cp936 -*-
#P89 6.1 懒惰即美德
# 输出菲波那契数列前10个数字
fibs = [0, 1]
for i in range(8):
fibs.append(fibs[-2] + fibs[-1])
print fibs
# 动态输出
fibs = [0, 1]
num = input('How many Fibonacci numbers do you want?')
for i in range(num - 2):
fibs.append(fibs[-2] + fibs[-1])
print fibs
raw_input("Press <enter>")
# -*- coding: cp936 -*-
#P90 6.3 创建函数
# 判断函数是否可调用
import math
x = 1
y = math.sqrt
print callable(x) #callable函数 判断函数是否可调用,Python3.0不再可用
print callable(y) #使用表达 hasattr(func, _call_) 代替
# 使用def定义函数
def hello(name): #定义函数
return 'Hello, ' + name + '!'
print hello('world')#调用函数
print hello('Gumby')
# 返回菲波那契数列列表的函数
def fibs(num): #定义函数
result = [0, 1]
for i in range(num - 2):
result.append(result[-2] + result[-1])
return result
print fibs(10) #调用函数
print fibs(15)
#6.3.1 文档化函数
def square(x):
'Calculates the square of the number x.' #文档字符串
return x*x
print square.__doc__ #访问文档字符串 doc两边各有2个下划线!
print square(5)
print help(square)
#6.3.2 并非真正函数的函数
# 指有些函数无返回值
def test():
print 'Thi is printed'
return
print 'This is not'
x = test()
print x
raw_input("Press <enter>")