Pascal’s Triangle in Python

Pascal’s Triangle in Python | Pascal’s Triangle is a pattern of triangles that are made based upon nCr. The property of Pascal’s triangle is “the sum of each adjacent two numbers of the previous row is the value of the number that is set just down on the next row.

In this article, we will learn, how to write a program in python programming to print or code a Pascal’s Triangle. 

Program to Print Pascal’s Triangle in Python Language

We can use two methods to print Pascal’s Triangle. Here we will make two programs, one using a function and the other without using a function.

Program to Print Pascal’s Triangle Without using a Function in Python Language

We will make a program using “for loop”. So, firstly we will take input from the user to enter the number of rows to present in Pascal’s Triangle. Then, we will create a list after that, we will implement a ‘nested for loop’ to make the pattern of Pascal’s triangle in Python Programming.

# Python Program to Print Pascal's Triangle
rows = int(input("Enter the number of rows: "))
l = []

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

for i in range(rows):
   print("   "*(rows-i), end=" ", sep=" ")
   for j in range(0, i+1):
      print("{0:6}".format(l[i][j]), end=" ", sep=" ")
   print()

Output:-

Enter the number of rows: 5
                          1 
                      1      1 
                  1      2      1 
              1      3      3      1 
          1      4      6      4      1 
Enter the number of rows: 7
                                  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

Program to Print Pascal’s Triangle Using a Function in Python Language

We will make the program using the function here. So, firstly we will take input from the user to enter the number of rows to be present in Pascal’s Triangle. Then, we will create a function called pascalTriangle() after that we will implement a nested for loop to make the pattern of Pascal’s triangle in Python Programming. 

# Python Program to Print Pascal's Triangle
def pascalTriangle(row, column):
   if (column == 1):
      return 1
   if (column == row):
      return 1
   upLeft = pascalTriangle(row-1, column-1)
   upRight = pascalTriangle(row-1, column)
   return upLeft+upRight

for r in range(1, 10):
   for c in range(1, r+1):
      print(pascalTriangle(r, c), end=" ")
   print("")

Output:-

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

So, here we discussed the Python Program to print Pascal’s Triangle using a function and without using the function. Hope you all find the article useful.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *