Fibonacci column
ordinary function to achieve
#普通函数 def fb(max): a,b=0,1 while a<max: print(a) a,b=b,a+b fb(100)
Recursive method
def fb1(max,a=1,b=1): if a<max: print(a) fb1(max,b,a+b) fb1(1000,77,88)
Recursive method, writing the most simple, but the least efficient, there will be a lot of double counting
def function(n): assert n >= 0, 'n > 0' if n<= 1: return n return function(n-1) + function(n-2) print(function(4)) for i in range(0,20): print(function(i),end=',')
Recursive method, recursive method, is gradually increasing, linear growth, if the huge amount of data, the more delay, the slower speed
def function(n): a,b = 0,1 for i in range(n): a,b = b,a+b return a print(function(3))
Generator implements
DEF FIB (max): A, B = 0,1 the while A < max: the yield A A, B = B, A + B fib_gt = FIB (100 ) # call generator generates the number of columns Print (Next (fib_gt)) Print ( next (fib_gt))
pyramid
int = n-(INPUT ( ' Enter your print layers need stars: ' )) for I in Range (. 1,. 1 n-+ ): Print ( ' ' * (n-- (I -. 1)) + ' * ' * (2 * i - 1) )
1 multiplication tables direction
for i in range(1,10): for j in range(1,i+1): d = i * j print('%d*%d=%-2d'%(i,j,d),end = ' ' ) print()
Direction twin
def hanshu(n): m = n sums = 0 for j in range(1,n+1): sums = m*j print("%d*%d=%-2d"%(m,j,sums),end = " ") print("") def hanshu1(): for i in range(9,0,-1): hanshu(i) hanshu1()
Direction three
def hanshu(n): m = n sums = 0 for k in range(0,10-n): print(" ",end = "") for j in range(1,n+1): sums = m*j print("%d*%d=%-2d"%(m,j,sums),end = " ") print("") def hanshu1(): for i in range(1,10): A poet - (I) hanshu1 ()
Direction four
def hanshu(n): for dix in range(10-n,0,-1): print(" ",end = "") sums = 0 m = n for j in range(1,n+1): sums = m*j print("%d*%d=%-2d"%(m,j,sums),end = " ") print("") def hanshu1(): for i in range(9,0,-1): A poet - (I) hanshu1 ()
done。