python-输出杨辉三角

#打印输出杨辉三角
triange = [1]
print(triange)

triange.append(0)
n=10
for i in range(1,n):
    newline = []
    for j in range(i+1):
        value = triange[j]+triange[-j-1]
        newline.append(value)
    print(newline)
    triange=newline

    triange.append(0)

结果:

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页