Python Program to Print (Generate) Pascal Triangle

In mathematics, Pascal's triangle is an array of the binomial coefficients. Each coefficients can be obtained by summing adjacent elements in preceding rows.

This Python program prints Pascal's Triangle up to n rows given by user.

Python Source Code: Pascal's Triangle


row = int(input("Enter number of rows: "))

space = 36

# empty list containg all 0s
a = [0] * 20 


print("\n\t\t\t\t*** PASCAL TRIANGLE ***\n")
for i in range(row):
    
    for spi in range(1,space+1):
        print(" ", end="")
    
    a[i] = 1
    
    for j in range(i+1):
        print('%6d' %(a[j]), end = "")
    
    for j in range(i,0,-1):
        a[j] = a[j] + a[j-1]
    
    space = space - 3
    
    print()

Output

Output of generated Pascal's Triangle in Python is:

Enter number of rows: 10

				*** PASCAL TRIANGLE ***

                                         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