In Python, we can easily create Pascal’s Triangle with a loop to create a multi-dimensional list for a given number of rows.

def pascalsTriangle(rows):
    t = []
    for i in range(rows):
        t.append([])
        t[i].append(1)
        for j in range(1,i):
            t[i].append(t[i-1][j-1]+t[i-1][j])
        if i != 0:        
            t[i].append(1)
    return t

print(pascalsTriangle(5))

#Output:
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]

To print a generated Pascal’s Triangle, we can loop over the triangle and print the elements.

def pascalsTriangle(rows):
    t = []
    for i in range(rows):
        t.append([])
        t[i].append(1)
        for j in range(1,i):
            t[i].append(t[i-1][j-1]+t[i-1][j])
        if i != 0:        
            t[i].append(1)
    return t

def printTriangle(t):
    printString = ""
    for i in range(0,len(t)):
        printString = " " * (len(t) - i)
        for j in range(0,len(t[i])):
            printString = printString + str(t[i][j]) + " "
        print(printString)

printTriangle(pascalsTriangle(5))

#Output:
     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1

Pascal’s Triangle is a very interesting construct from the field of mathematics. Pascal’s Triangle is a triangular array constructed from summing adjacent elements in preceding rows.

In Python, we can easily create Pascal’s Triangle for a given number of rows with a for loop that loops over the number of rows and sums the adjacent elements from the previous row.

We can easily define a function which will create a multi-dimensional list with each row of the triangle.

Below is an simple Python function which generates Pascal’s Triangle for a given number of rows.

def pascalsTriangle(rows):
    t = []
    for i in range(rows):
        t.append([])
        t[i].append(1)
        for j in range(1,i):
            t[i].append(t[i-1][j-1]+t[i-1][j])
        if i != 0:        
            t[i].append(1)
    return t

print(pascalsTriangle(5))

#Output:
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]

How to Print Pascal’s Triangle in Python

The example above only shows how to create the numbers in Pascal’s Triangle. We can use Python to print a created Pascal’s Triangle by looping over each row and printing the elements.

We can print Pascal’s Triangle in a triangle so you can easily visualize the pattern and beauty of the construct.

Below are two examples printing Pascal’s Triangle with 5 and 10 rows to the console in Python.

def pascalsTriangle(rows):
    t = []
    for i in range(rows):
        t.append([])
        t[i].append(1)
        for j in range(1,i):
            t[i].append(t[i-1][j-1]+t[i-1][j])
        if i != 0:        
            t[i].append(1)
    return t

def printTriangle(t):
    printString = ""
    for i in range(0,len(t)):
        printString = " " * (len(t) - i)
        for j in range(0,len(t[i])):
            printString = printString + str(t[i][j]) + " "
        print(printString)

printTriangle(pascalsTriangle(5))
print()
printTriangle(pascalsTriangle(10))

#Output:
     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1

          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

Hopefully this article has been useful for you to learn how to create Pascal’s Triangle in Python.

Categorized in:

Python,

Last Update: February 26, 2024