Python Program to print the pascal’s triangle for n number of rows


February 21, 2022, Learn eTutorial
1428

What is the pascals triangle?

Pascal's triangle is defined as a triangular array of binomial coefficients. Pascal's triangle is symmetrical, which means the triangle's left side will be the same as the triangle's right side. In this python program, we need to print one of the most interesting number patterns called pascal's triangle. Pascal triangle is in a triangle shape pattern of numbers with the topmost is a 1. Then each number is the sum of two numbers above the resultant number. 

interchange diagonals of matrix

How to solve pascal's triangle in python?

To implement this pascal's triangle logic into the python program, we accept the number of rows from the user. Then we initialize a list, and we append 1 to all the sublists in the triangle. Now we are opening a for loop in python from 1 to row number. Then we calculate the sub-list elements by using the sum of previous elements of the triangle, ie. a[i-1][j-1]+a[i-1][j]

Let us take an example of the second row of the pascal triangle, then the 'i' will be 2, and j will be 1 for the first element in row 2. So using the formula [2-1][1-1]+[2-1][1], which is equal to 1. Then finally, we use another for loop for printing the pascal's triangle in the correct pattern. 

We are using a format method in python language here to print in the proper way of pascal's triangle. The format () method is a built-in method in python, which returns a formatted output specified in the format parameter. The syntax for format() the method is "format(value,[format_spec])" format method takes two parameters in which one is the value to be formatted, and the other is the specifier, which mentions how the value to be formatted in python. 

ALGORITHM

STEP 1: Accept the number of rows from the user. 

STEP 2: Initialize a list using the python method. 

STEP 3: Open a for loop till the number n and append all elements to 1 to all sublists. 

STEP 4: Open the nested for loop to calculate each element in the sublist using the formula (a[i-1][j-1]+a[i-1][j]) [ we described how the formula works in description]

STEP 5: Use another for loop to print the elements, and we use the format method to print it pascal's triangle format using python basic syntax.


To solve this pascals triangle problem, we are using the below concepts of python programming, please refer these topics to get a better understanding

Python Source Code

                                          n=int(input("Enter number of rows: "))
a=[]
for i in range(n):
    a.append([])
    a[i].append(1)
    for j in range(1,i):
        a[i].append(a[i-1][j-1]+a[i-1][j])
    if(n!=0):
        a[i].append(1)
print("Pascal's triangle of",n,"rows is")
for i in range(n):
    print("   "*(n-i),end=" ",sep=" ")
    for j in range(0,i+1):
        print('{0:6}'.format(a[i][j]),end=" ",sep=" ")
    print()
                                      

OUTPUT

Enter number of rows: 3
Pascal's triangle of 3 rows is
                1 
            1      1 
         1     2     1