当前位置:主页 > 软件编程 > Python代码 >

使用python打印十行杨辉三角过程详解

时间:2021-01-08 12:21:22 | 栏目:Python代码 | 点击:

杨辉三角,是二项式系数在三角形中的一种几何排列

性质5和性质7是杨辉三角的基本性质,是研究杨辉三角其他规律的基础。

代码

num=input('请输入行数:')
num =int(num)

list1 =[] #list 用来保存杨辉三角
for n in range(num):
  row =[1] #保存行
  list1.append(row)

  if n ==0:
    print(row)
    continue
  for m in range(1,n):
    row.append(list1[n - 1][m - 1] + list1[n - 1][m])
  row.append(1)

  print(row)

结果

您可能感兴趣的文章:

相关文章