python活力练习Day13
Day13:给出一个整数n,求出前n行的杨辉三角
1 def numrows(n): 2 s1 = [] #存储前n行的杨辉三角数 3 for i in range(1,n+1): 4 s = [1 for j in range(i)] #存储第i行的个数 先设置为1 5 if len(s) > 2: 6 for i in range(1,len(s)-1): 7 s[i] = s1[-1][i-1] + s1[-1][i] 8 s1.append(s) 9 else: 10 s1.append(s) 11 return s1 12 13 print(numrows(6))
输出结果: