Python Program to Print Pascal Triangle

In this tutorial, we will discuss how we can print the Pascal triangle using the Python program. But first, let's understand what the Pascal triangle is.

Introduction

Pascal triangle is an exciting concept of mathematics where a triangular array is formed by summing adjacent elements in the preceding row. In simple words, every number is generated by taking up the sum of the adjacent row, and the outside edges of a triangle are always 1. It is named after the famous French mathematician Blaise Pascal. Below is the representation of the Pascal triangle.

Python Program to Print Pascal Triangle

In the above image, the first line is 1. The second line contains 2 one, and the third line has 1 2 1, which is formed by taking up the sum of the above line. So it follows the alternate pattern in an entire triangle and so on.

Algorithm to Print Pascal's Triangle in Python

Below are the step used to print the Pascal triangle.

Step - 1: Taking input from the user to get the number of rows.

Step - 2: Declare an empty list that will store the values.

Step - 3: Using for loop, which will iterate through 0 to n - 1, append the sub-lists to the list.

Step - 4: Now append 1 to the list.

Step - 5: Now, use for loop to define the value of the number inside the triangle adjacent row.

Step - 6: Print Pascal triangle according to the format.

Step - 7: Exit

Let's understand the following implementation of algorithm into Python program.

Program

Output:

Case - 1

Enter the number: 4
          1 
      1      1 
  1      2      1 
1      3      3      1

We run the program one more time.

Case - 2:

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

Explanation -

In the above program, we have taken several rows from the user as input. We have declared an empty list. Then we used for loop, which appended the sub-list into an empty list. The next one is appended to all sub-lists. The inner for loop defined the number inside the triangle is the sum of the above adjacent row. In the end, we have used for loop to print Pascal's triangle according to a format.

Method - 2:

We can use another method by the following the binomial coefficient, all the line starts with 1 and the ith entry in a row number line is Binomial Coefficient C(line, i). The formula is given below.

Example -

The above method's time complexity is 0(N2).

Method - 3

This method is optimized way of printing Pascal triangle. This method is based on the power of 11.

Example -

Output:

Case - 1:

Enter the number of rows: 5
     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1

Case - 2:

Enter the number of rows: 6  
       1
      1 1
     1 2 1
    1 3 3 1
   1 4 6 4 1
  1 6 1 0 5 1

As we can see in the above code when we enter the number of rows five. It gave the wrong output. That means it works upto n< = 5 only. It takes less time to execute 0(N).