Yang's Blog

Python学习笔记

Python 中简单的运算

加(+)

1
2
3
4
def add(a,b):
return a + b
print add(1, 2) // 3

减(-)

1
2
3
4
def sub(a, b):
return a - b
print add(2, 1) // 1

乘(×)

1
2
3
4
def sun(a, b):
return a * b
print add(1, 2) // 2

除(÷)

1
2
3
4
def divided(a, b):
return a / b
print add(2, 1) // 2

Python 实现斐波那契数列

方法一

1
2
3
4
5
6
def fibo(num):
numList = [0,1]
for i in range(num - 2):
numList.append(numList[-2] + numList[-1])
return numList

方法二

1
2
3
4
5
6
def fibo(n):
x, y = 0, 1
while(n):
x,y,n = y, x+y, n - 1
return x

方法三

1
2
3
4
5
6
7
8
9
10
def fibo(n):
def fib_iter(n,x, y):
if n == 0:
return x
else:
return fib_iter(n-1, y, x+y)
return fib_iter(n, 0, 1)
print fibo(3)

其实写我博客这是为了记录一些好玩的东西,做笔记也是一样的

Enjoy it ? Donate me ! 欣赏此文?求鼓励,求支持!