Python打印杨辉三角

Python打印杨辉三角

def Triangle(n):
    """打印n层杨辉三角"""
    if n <= 0:
        print("请输入大于0的整数")
        return None
    res = [[1]]
    for i in range(n):
        print(res[-1])
        res.append([1]+[e1+e2 for e1,e2 in zip(res[-1][:-1], res[-1][1:])]+[1])
    return res

if __name__ == '__main__':
    Triangle(10)

你可能感兴趣的:(Python基础)